{"size": 2292, "ext": "js", "max_stars_repo_path": "src/common/ErrorMessage.js", "max_stars_repo_name": "pbrudny/blockey-react", "max_stars_repo_stars_event_min_datetime": "2018-06-17T17:32:10.000Z", "max_stars_repo_stars_event_max_datetime": "2018-07-27T14:22:11.000Z", "max_issues_repo_path": "src/common/ErrorMessage.js", "max_issues_repo_name": "pbrudny/blockey-react", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/common/ErrorMessage.js", "max_forks_repo_name": "pbrudny/blockey-react", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.6288659794, "max_line_length": 97, "alphanum_fraction": 0.544938918}
import React from 'react';
import Error from './Error';

/**
 * Axios when self-failures will return Object like
 action: {
      error: {
          data: "...",
          status: 0,
      }
  }
 * when request fails with server -
 action: {
      error: {
          response: {
              data: {},
              status: 404,
              ...
          }
      }
  }
 */

const ErrorMessage = ({ error: { response } }) => {
  if (!response) {
    return null;
  }

  function getError() {

    if (response.status === 400 && typeof response.data === 'object') {
      return (
        Object.keys(response.data).map((key) => {
          if(key === 'non_field_errors') {
            return response.data[key].map((item) => <p className="error-block">{item}</p> )
          }else {
            return <p className="error-block">{`${key} - ${response.data[key]}`}</p>
          }
        })
      )
    }
    if (response.status === 404 && typeof response.data === 'string') {
      return <p className="error-block">404 - could not find the element</p>;
    }

    if (response.status >= 500) {
      return <p className="error-block">{response.status} - internal server error, try again</p>;
    }

    if (typeof response.data === 'string') { // if string it's most likely django html
      return <p className="error-block">{response.status}: Something went wrong</p>
    }

    return flatten(mapToStringArray(response.data)).map(
      ([key, value], i) => <p className="error-block" key={i}>{key}: {value}</p>
    );
  }

  const errors = getError();
  return errors.length ? <Error>{errors}</Error> : null;
};

ErrorMessage.defaultProps = {
  error: {
    response: {
      data: {},
    },
  },
};

export default ErrorMessage;

function mapToStringArray(errorData) {
  return Object.entries(errorData).map(([key, value]) => {
    if (Array.isArray(value)) {
      value = value[0];
    }

    if (value.constructor === Object) {
      return mapToStringArray(value);
    }

    return [key, value];
  });
}

function flatten(arr) {
  let flatArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] instanceof Array && typeof arr[i][1] !== 'string') {
      flatArr = flatArr.concat(flatten(arr[i]));
    } else {
      flatArr.push(arr[i]);
    }
  }
  return flatArr;
}