@overmap-ai/core 1.0.25-fix-api-error-messages.3 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3315,6 +3315,38 @@ const defaultStore = configureStore({
3315
3315
  });
3316
3316
  }
3317
3317
  });
3318
+ function extractResponseFromError(error) {
3319
+ function isResponse(response) {
3320
+ const knownKeys = ["ok", "redirect", "clientError", "serverError", "error"];
3321
+ return typeof response === "object" && response !== null && knownKeys.every((key) => key in response);
3322
+ }
3323
+ if (isResponse(error))
3324
+ return error;
3325
+ if (typeof error === "object" && error !== null) {
3326
+ const typedError = error;
3327
+ if (isResponse(typedError.response))
3328
+ return typedError.response;
3329
+ if (typedError.response && isResponse(typedError.response.response))
3330
+ return typedError.response.response;
3331
+ }
3332
+ return void 0;
3333
+ }
3334
+ function extractErrorMessage(errorRes, err) {
3335
+ if (errorRes == null ? void 0 : errorRes.body) {
3336
+ if (typeof errorRes.body === "object") {
3337
+ if (typeof errorRes.body.error === "string")
3338
+ return errorRes.body.error;
3339
+ if (typeof errorRes.body.message === "string")
3340
+ return errorRes.body.message;
3341
+ } else if (typeof errorRes.body === "string")
3342
+ return errorRes.body;
3343
+ } else if (errorRes == null ? void 0 : errorRes.text) {
3344
+ return errorRes.text;
3345
+ } else if (err instanceof Error) {
3346
+ return err.message;
3347
+ }
3348
+ return void 0;
3349
+ }
3318
3350
  async function performRequest(action, client) {
3319
3351
  async function checkToken() {
3320
3352
  if (client.auth.tokenIsExpiringSoon()) {
@@ -3409,13 +3441,8 @@ async function performRequest(action, client) {
3409
3441
  try {
3410
3442
  return await requestToSend.query(queryParams);
3411
3443
  } catch (error) {
3412
- console.debug("TYPE OF ERROR:", typeof error, error instanceof Error, error instanceof Response);
3413
- if (error instanceof Error) {
3414
- console.error(error.message);
3415
- }
3416
- console.error(error);
3417
- const originalError = error;
3418
- const status = originalError["status"];
3444
+ const errorResponse = extractResponseFromError(error);
3445
+ const status = errorResponse == null ? void 0 : errorResponse.status;
3419
3446
  if (status === 401) {
3420
3447
  console.debug("Forbidden; renewing tokens and retrying.");
3421
3448
  try {
@@ -3431,25 +3458,15 @@ async function performRequest(action, client) {
3431
3458
  } else {
3432
3459
  console.warn("No signed-in user to sign out.");
3433
3460
  }
3434
- throw new APIError("You have been signed out due to inactivity.", originalError, {
3461
+ throw new APIError("You have been signed out due to inactivity.", errorResponse, {
3435
3462
  discard: true
3436
3463
  });
3437
3464
  }
3438
3465
  }
3439
- const members = ["message", "text", "body", "error"];
3440
- const apiErrorMessage = members.map((member) => originalError[member]).find((member) => member !== void 0);
3441
- throw new APIError(
3442
- typeof apiErrorMessage === "string" ? apiErrorMessage : (
3443
- // TODO: Error codes for all APIErrors.
3444
- // TODO: Constant for all messages.
3445
- "An unexpected error occurred."
3446
- ),
3447
- // TODO: Update the type in APIError to accept Error as well, or figure out when it's this or that.
3448
- originalError,
3449
- {
3450
- discard: discardStatuses.includes(status)
3451
- }
3452
- );
3466
+ const apiErrorMessage = extractErrorMessage(errorResponse, error) || "An unexpected error occurred.";
3467
+ throw new APIError(apiErrorMessage, errorResponse, {
3468
+ discard: discardStatuses.includes(status)
3469
+ });
3453
3470
  }
3454
3471
  }
3455
3472
  class MiddlewareChainerPrivate {