@overmap-ai/core 1.0.49-fix-error-messaging.3 → 1.0.49-fix-error-messaging.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.
@@ -217,50 +217,60 @@ class OutboxCoordinator {
217
217
  this.requestAttemptCounter[uuid] = (this.requestAttemptCounter[uuid] || 0) + 1;
218
218
  }
219
219
  }
220
+ const UNKNOWN_ERROR_MESSAGE = "An unknown error occurred";
221
+ const MAX_ERROR_MESSAGE_LENGTH = 500;
222
+ const _SPECIAL_KEYS = ["non_field_errors", "detail"];
220
223
  function extractErrorMessage(errorRes, err) {
224
+ let ret;
221
225
  if (errorRes == null ? void 0 : errorRes.body) {
222
226
  if (typeof errorRes.body === "object") {
223
- if (typeof errorRes.body.error === "string")
224
- return errorRes.body.error;
225
- if (typeof errorRes.body.message === "string")
226
- return errorRes.body.message;
227
- try {
228
- return Object.entries(errorRes.body).map(([key, value]) => {
229
- if (typeof value === "string") {
230
- if (key === "non_field_errors")
231
- return value;
232
- return `${key}: ${value}`;
233
- }
234
- if (Array.isArray(value)) {
235
- if (key === "non_field_errors")
236
- return value.join("\n");
237
- return value.map((v) => `${key}: ${v}`).join("\n");
238
- }
239
- return `${key}: ${JSON.stringify(value)}`;
240
- }).join("\n");
241
- } catch (e) {
242
- console.error("Failed to extract error message from response body", e);
227
+ const responseBody = errorRes.body;
228
+ if (typeof responseBody.error === "string") {
229
+ ret = responseBody.error;
230
+ } else if (typeof responseBody.message === "string") {
231
+ ret = responseBody.message;
232
+ } else if (responseBody.body) {
233
+ try {
234
+ ret = Object.entries(responseBody.body).map(([key, value]) => {
235
+ if (typeof value === "string") {
236
+ if (_SPECIAL_KEYS.includes(key))
237
+ return value;
238
+ return `${key}: ${value}`;
239
+ }
240
+ if (Array.isArray(value)) {
241
+ if (_SPECIAL_KEYS.includes(key))
242
+ return value.join("\n");
243
+ return value.map((v) => `${key}: ${v}`).join("\n");
244
+ }
245
+ return `${key}: ${JSON.stringify(value)}`;
246
+ }).join("\n");
247
+ } catch (e) {
248
+ console.error("Failed to extract error message from response body", e);
249
+ }
243
250
  }
244
- } else if (typeof errorRes.body === "string")
245
- return errorRes.body;
251
+ } else if (typeof errorRes.body === "string") {
252
+ ret = errorRes.body;
253
+ }
246
254
  } else if (errorRes == null ? void 0 : errorRes.text) {
247
- return errorRes.text;
255
+ ret = errorRes.text;
248
256
  } else if (err instanceof Error) {
249
- return err.message;
257
+ ret = err.message;
250
258
  }
251
- return void 0;
259
+ if (!ret || ret.length > MAX_ERROR_MESSAGE_LENGTH) {
260
+ return UNKNOWN_ERROR_MESSAGE;
261
+ }
262
+ return ret;
252
263
  }
253
264
  class APIError extends Error {
254
265
  constructor(options) {
255
- const unknownMessage = "An unknown error occurred";
256
- super(unknownMessage);
266
+ super(UNKNOWN_ERROR_MESSAGE);
257
267
  // NOTE: Needs to conform to NetworkError in @redux-offline/redux-offline, which has `status` and `response`.
258
268
  __publicField(this, "status");
259
269
  __publicField(this, "response");
260
270
  __publicField(this, "message");
261
271
  __publicField(this, "options");
262
272
  const { response, innerError } = options;
263
- this.message = options.message ?? extractErrorMessage(response, innerError) ?? unknownMessage;
273
+ this.message = options.message ?? extractErrorMessage(response, innerError) ?? UNKNOWN_ERROR_MESSAGE;
264
274
  this.status = (response == null ? void 0 : response.status) ?? 0;
265
275
  this.response = response;
266
276
  options.discard = options.discard ?? false;