{"size": 1115, "ext": "js", "max_stars_repo_path": "src/lib/util/error.js", "max_stars_repo_name": "3l1k/web", "max_stars_repo_stars_event_min_datetime": "2019-05-23T22:07:04.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-30T08:23:15.000Z", "max_issues_repo_path": "src/lib/util/error.js", "max_issues_repo_name": "3l1k/web", "max_issues_repo_issues_event_min_datetime": "2019-04-08T23:58:44.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T20:36:42.000Z", "max_forks_repo_path": "src/lib/util/error.js", "max_forks_repo_name": "3l1k/web", "max_forks_repo_forks_event_min_datetime": "2020-02-08T21:16:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-22T10:25:01.000Z"}
{"max_stars_count": 32, "max_issues_count": 232, "max_forks_count": 18, "avg_line_length": 20.2727272727, "max_line_length": 65, "alphanum_fraction": 0.5775784753}
import FetchError from "/lib/error/FetchError";
import ReactError from "/lib/error/ReactError";
import { isApolloError } from "apollo-client/errors/ApolloError";

export const unwrapError = error => {
  if (error instanceof Error) {
    const meta = {};

    if (error instanceof ReactError) {
      return {
        componentStack: error.componentStack,
        ...unwrapError(error.original),
      };
    }

    if (isApolloError(error) && error.networkError) {
      return unwrapError(error.networkError);
    }

    if (error instanceof FetchError) {
      if (error.original) {
        return {
          url: error.url,
          statusCode: error.statusCode,
          ...unwrapError(error.original),
        };
      }

      meta.url = error.url;
      meta.statusCode = error.statusCode;
    }

    return {
      ...meta,
      name: error.name,
      stack: error.stack,
      message: error.message,
    };
  }

  let message;

  try {
    message = `${JSON.stringify(error, null, 2)}`;
  } catch (e) {
    message = `${error}`;
  }

  return {
    message,
    stack: "",
    name: "Error",
  };
};
