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

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,8 +3315,39 @@ 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
- var _a2;
3320
3351
  async function checkToken() {
3321
3352
  if (client.auth.tokenIsExpiringSoon()) {
3322
3353
  await client.auth.renewTokens();
@@ -3410,10 +3441,9 @@ async function performRequest(action, client) {
3410
3441
  try {
3411
3442
  return await requestToSend.query(queryParams);
3412
3443
  } catch (error) {
3413
- console.debug("TYPE OF ERROR:", typeof error, error instanceof Error, error instanceof Response);
3414
- console.error(error);
3415
- const originalError = error;
3416
- if (originalError.status === 401) {
3444
+ const errorResponse = extractResponseFromError(error);
3445
+ const status = errorResponse == null ? void 0 : errorResponse.status;
3446
+ if (status === 401) {
3417
3447
  console.debug("Forbidden; renewing tokens and retrying.");
3418
3448
  try {
3419
3449
  await client.auth.renewTokens();
@@ -3428,29 +3458,15 @@ async function performRequest(action, client) {
3428
3458
  } else {
3429
3459
  console.warn("No signed-in user to sign out.");
3430
3460
  }
3431
- throw new APIError("You have been signed out due to inactivity.", originalError, { discard: true });
3461
+ throw new APIError("You have been signed out due to inactivity.", errorResponse, {
3462
+ discard: true
3463
+ });
3432
3464
  }
3433
3465
  }
3434
- const apiErrorMessage = ((_a2 = originalError.body) == null ? void 0 : _a2.error) ?? originalError.text ?? originalError.error;
3435
- console.log(
3436
- "API error:",
3437
- originalError,
3438
- originalError.error,
3439
- originalError.text,
3440
- originalError.body,
3441
- originalError.status
3442
- );
3443
- throw new APIError(
3444
- typeof apiErrorMessage === "string" ? apiErrorMessage : (
3445
- // TODO: Error codes for all APIErrors.
3446
- // TODO: Constant for all messages.
3447
- originalError.text || "An unexpected error occurred."
3448
- ),
3449
- originalError,
3450
- {
3451
- discard: discardStatuses.includes(originalError.status)
3452
- }
3453
- );
3466
+ const apiErrorMessage = extractErrorMessage(errorResponse, error) || "An unexpected error occurred.";
3467
+ throw new APIError(apiErrorMessage, errorResponse, {
3468
+ discard: discardStatuses.includes(status)
3469
+ });
3454
3470
  }
3455
3471
  }
3456
3472
  class MiddlewareChainerPrivate {