@overmap-ai/core 1.0.48 → 1.0.49-fix-error-messaging.1

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.
@@ -208,18 +208,54 @@ var __publicField = (obj, key, value) => {
208
208
  this.requestAttemptCounter[uuid2] = (this.requestAttemptCounter[uuid2] || 0) + 1;
209
209
  }
210
210
  }
211
+ function extractErrorMessage(errorRes, err) {
212
+ if (errorRes == null ? void 0 : errorRes.body) {
213
+ if (typeof errorRes.body === "object") {
214
+ if (typeof errorRes.body.error === "string")
215
+ return errorRes.body.error;
216
+ if (typeof errorRes.body.message === "string")
217
+ return errorRes.body.message;
218
+ try {
219
+ return Object.entries(errorRes.body).map(([key, value]) => {
220
+ if (typeof value === "string") {
221
+ if (key === "non_field_errors")
222
+ return value;
223
+ return `${key}: ${value}`;
224
+ }
225
+ if (Array.isArray(value)) {
226
+ if (key === "non_field_errors")
227
+ return value.join("\n");
228
+ return value.map((v) => `${key}: ${v}`).join("\n");
229
+ }
230
+ return `${key}: ${JSON.stringify(value)}`;
231
+ }).join("\n");
232
+ } catch (e) {
233
+ console.error("Failed to extract error message from response body", e);
234
+ }
235
+ } else if (typeof errorRes.body === "string")
236
+ return errorRes.body;
237
+ } else if (errorRes == null ? void 0 : errorRes.text) {
238
+ return errorRes.text;
239
+ } else if (err instanceof Error) {
240
+ return err.message;
241
+ }
242
+ return void 0;
243
+ }
211
244
  class APIError extends Error {
212
- constructor(message, response, options) {
213
- super(response == null ? void 0 : response.text);
245
+ constructor(options) {
246
+ const unknownMessage = "An unknown error occurred";
247
+ super(unknownMessage);
214
248
  // NOTE: Needs to conform to NetworkError in @redux-offline/redux-offline, which has `status` and `response`.
215
249
  __publicField(this, "status");
216
- __publicField(this, "message");
217
250
  __publicField(this, "response");
251
+ __publicField(this, "message");
218
252
  __publicField(this, "options");
219
- this.message = message;
253
+ const { response, innerError } = options;
254
+ this.message = options.message ?? extractErrorMessage(response, innerError) ?? unknownMessage;
220
255
  this.status = (response == null ? void 0 : response.status) ?? 0;
221
256
  this.response = response;
222
- this.options = options ?? { discard: false };
257
+ options.discard = options.discard ?? false;
258
+ this.options = options;
223
259
  }
224
260
  }
225
261
  class DeferredPromise {
@@ -4168,35 +4204,6 @@ var __publicField = (obj, key, value) => {
4168
4204
  }
4169
4205
  return void 0;
4170
4206
  }
4171
- function extractErrorMessage(errorRes, err) {
4172
- if (errorRes == null ? void 0 : errorRes.body) {
4173
- if (typeof errorRes.body === "object") {
4174
- if (typeof errorRes.body.error === "string")
4175
- return errorRes.body.error;
4176
- if (typeof errorRes.body.message === "string")
4177
- return errorRes.body.message;
4178
- try {
4179
- return Object.entries(errorRes.body).map(([key, value]) => {
4180
- if (typeof value === "string") {
4181
- return `${key}: ${value}`;
4182
- }
4183
- if (Array.isArray(value)) {
4184
- return value.map((v) => `${key}: ${v}`).join("\n");
4185
- }
4186
- return `${key}: ${JSON.stringify(value)}`;
4187
- }).join("\n");
4188
- } catch (e) {
4189
- console.error("Failed to extract error message from response body", e);
4190
- }
4191
- } else if (typeof errorRes.body === "string")
4192
- return errorRes.body;
4193
- } else if (errorRes == null ? void 0 : errorRes.text) {
4194
- return errorRes.text;
4195
- } else if (err instanceof Error) {
4196
- return err.message;
4197
- }
4198
- return void 0;
4199
- }
4200
4207
  async function performRequest(action, client) {
4201
4208
  async function checkToken() {
4202
4209
  if (client.auth.tokenIsExpiringSoon()) {
@@ -4299,8 +4306,11 @@ var __publicField = (obj, key, value) => {
4299
4306
  console.warn("No signed-in user to sign out.");
4300
4307
  }
4301
4308
  await client.auth.logout();
4302
- throw new APIError("You have been signed out due to inactivity.", errorResponse, {
4303
- discard: true
4309
+ throw new APIError({
4310
+ message: "You have been signed out due to inactivity.",
4311
+ response: errorResponse,
4312
+ discard: true,
4313
+ innerError: error2
4304
4314
  });
4305
4315
  }
4306
4316
  console.debug("Forbidden; renewing tokens and retrying.");
@@ -4308,10 +4318,7 @@ var __publicField = (obj, key, value) => {
4308
4318
  console.debug("Successfully renewed tokens; retrying request.");
4309
4319
  return requestToSend.query(queryParams);
4310
4320
  }
4311
- const apiErrorMessage = extractErrorMessage(errorResponse, error2) || "An unexpected error occurred.";
4312
- throw new APIError(apiErrorMessage, errorResponse, {
4313
- discard: discardStatuses.includes(status)
4314
- });
4321
+ throw new APIError({ response: errorResponse, innerError: error2, discard: discardStatuses.includes(status) });
4315
4322
  }
4316
4323
  }
4317
4324
  class MiddlewareChainerPrivate {
@@ -4522,18 +4529,29 @@ var __publicField = (obj, key, value) => {
4522
4529
  if (response) {
4523
4530
  promise.resolve(response.body);
4524
4531
  } else {
4525
- const error2 = new APIError(
4526
- "Could not get a response from the server.",
4532
+ const error2 = new APIError({
4533
+ message: "Could not get a response from the server.",
4527
4534
  response,
4528
- {
4529
- discard: true
4530
- }
4531
- );
4535
+ discard: true
4536
+ });
4532
4537
  promise.reject(error2);
4533
4538
  }
4534
4539
  };
4535
4540
  const errorHandler = (error2) => {
4536
- error2.options.discard = true;
4541
+ if (error2 instanceof APIError) {
4542
+ error2.options.discard = true;
4543
+ } else {
4544
+ console.error(
4545
+ "Received an unexpected error while processing a request:",
4546
+ error2,
4547
+ "\nConverting error to APIError and discarding."
4548
+ );
4549
+ error2 = new APIError({
4550
+ message: "An error occurred while processing the request.",
4551
+ innerError: error2,
4552
+ discard: true
4553
+ });
4554
+ }
4537
4555
  promise.reject(error2);
4538
4556
  };
4539
4557
  innerPromise.then(successOrUndefinedHandler, errorHandler);
@@ -7160,8 +7178,7 @@ var __publicField = (obj, key, value) => {
7160
7178
  blockers: [],
7161
7179
  blocks: []
7162
7180
  });
7163
- const organizationAccesses = result;
7164
- store.dispatch(setOrganizationAccesses(organizationAccesses));
7181
+ store.dispatch(setOrganizationAccesses(result));
7165
7182
  }
7166
7183
  }
7167
7184
  const cachedRequestPromises = {};