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