export function createDisplayError()

in packages/graph-explorer/src/utils/createDisplayError.ts [20:116]


export function createDisplayError(error: any): DisplayError {
  const data =
    error instanceof NetworkError
      ? error.data
      : typeof error === "object"
        ? error
        : null;
  if (data != null) {
    // Bad connection configuration
    if (data.code === "ECONNREFUSED" || data.cause?.code === "ECONNREFUSED") {
      return {
        title: "Connection refused",
        message: "Please check your connection and try again.",
      };
    }
    if (data.code === "ECONNRESET" || data.cause?.code === "ECONNRESET") {
      return {
        title: "Connection reset",
        message: "Please check your connection and try again.",
      };
    }
    if (
      data.code === "ERR_INVALID_URL" ||
      data.cause?.code === "ERR_INVALID_URL"
    ) {
      return {
        title: "Invalid URL",
        message:
          "Please check the database URL in the connection and try again.",
      };
    }
    if (
      data.code === "TimeLimitExceededException" ||
      data.cause?.code === "TimeLimitExceededException"
    ) {
      // Server timeout
      return {
        title: "Deadline exceeded",
        message:
          "Increase the query timeout in the DB cluster parameter group, or retry the request.",
      };
    }

    // Malformed query
    if (
      data.code === "MalformedQueryException" ||
      data.cause?.code === "MalformedQueryException"
    ) {
      return {
        title: "Malformed Query",
        message:
          "The executed query was rejected by the database. It is possible the query structure is not supported by your database.",
      };
    }
  }

  if (error instanceof Error) {
    // Fetch timeout
    if (error.name === "AbortError") {
      return {
        title: "Request cancelled",
        message: "The request exceeded the configured timeout length.",
      };
    }
    if (error.name === "TimeoutError") {
      return {
        title: "Fetch Timeout Exceeded",
        message: "The request exceeded the configured fetch timeout.",
      };
    }

    // Internet issues
    if (error.name === "TypeError" && error.message === "Failed to fetch") {
      return {
        title: "Connection Error",
        message: "Please check your connection and try again.",
      };
    }
  }

  if (error instanceof NetworkError) {
    if (error.statusCode === 429) {
      return {
        title: "Too Many Requests",
        message:
          "The database is currently overloaded. Please try again later.",
      };
    }

    return {
      title: `Network Response ${error.statusCode}`,
      message:
        extractMessageFromData(error.data) ?? defaultDisplayError.message,
    };
  }
  return defaultDisplayError;
}