import { isArray, isObject } from './../utils/isUtil';
import { isNotEmptyArray, isNotEmptyObject } from './isEmptyUtil';

export const mergeErrors = ({ error, errorArray, log }) => {
  const debug = false;
  debug && console.log(`🐛 ~ [${log}] error`, error);
  debug && console.log(`🐛 ~ [${log}] error type`, typeof error);
  debug && console.log(`🐛 ~ [${log}] error JSON`, JSON.stringify(error));
  debug && console.log(`🐛 ~ [${log}] errorArray`, errorArray);
  debug && log && console.log(`🐛 ~ file: errorAxiosUtil.js ~ [${log}]`);
  let output = [];
  const localErrorArray = errorArray;
  if (
    error &&
    isObject(error) &&
    isNotEmptyObject(error) &&
    localErrorArray &&
    isNotEmptyArray(localErrorArray)
  ) {
    if (
      localErrorArray.some(s => s.config) &&
      !localErrorArray.some(s => s.config.url === error.config.url)
    ) {
      debug && log && console.log(`🐛 ~ file: errorAxiosUtil.js ~ [${log}] ~ mergeErrors ~ case 1`);
      localErrorArray.push(error);
    }
    output = localErrorArray;
  } else if (error && localErrorArray && isArray(localErrorArray)) {
    localErrorArray.push({
      error,
    });
    debug && log && console.log(`🐛 ~ file: errorAxiosUtil.js ~ [${log}] ~ mergeErrors ~ case 2`);
    output = localErrorArray;
    debug && console.log('🐛 ~ file: errorAxiosUtil.js ~ line 32 ~ mergeErrors ~ output', output);
  }
  return output;
};

@@ -1393,8 +1393,710 @@
 put;%0A%7D;%0A
+%0Aexport const handleErrorAxios = err =%3E %7B%0A  // See: https://github.com/axios/axios#handling-errors%0A  let explanation = 'The error was not even handled';%0A  if (err.response) %7B%0A    explanation =%0A      'The request was made and the server responded with a status code that falls out of the range of 2xx';%0A  %7D else if (err.request) %7B%0A    // %60err.request%60 is an instance of XMLHttpRequest in the browser and an instance of%0A    // http.ClientRequest in node.js%0A    explanation = 'The request was made but no response was received';%0A  %7D else %7B%0A    explanation =%0A      'The data is invalid, empty or null - or something happened while setting up the request.';%0A  %7D%0A  return %7B%0A    err,%0A    explanation,%0A  %7D;%0A%7D;%0A

Add function that handles errors from 'axios', according to its API