@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.
- package/dist/overmap-core.js +38 -28
- package/dist/overmap-core.js.map +1 -1
- package/dist/overmap-core.umd.cjs +38 -28
- package/dist/overmap-core.umd.cjs.map +1 -1
- package/package.json +1 -1
|
@@ -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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
242
|
+
} else if (typeof errorRes.body === "string") {
|
|
243
|
+
ret = errorRes.body;
|
|
244
|
+
}
|
|
237
245
|
} else if (errorRes == null ? void 0 : errorRes.text) {
|
|
238
|
-
|
|
246
|
+
ret = errorRes.text;
|
|
239
247
|
} else if (err instanceof Error) {
|
|
240
|
-
|
|
248
|
+
ret = err.message;
|
|
241
249
|
}
|
|
242
|
-
|
|
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
|
-
|
|
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) ??
|
|
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;
|