@overmap-ai/core 1.0.49-fix-error-messaging.2 → 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;
@@ -4313,10 +4323,20 @@ var __publicField = (obj, key, value) => {
4313
4323
  innerError: error2
4314
4324
  });
4315
4325
  }
4316
- console.debug("Forbidden; renewing tokens and retrying.");
4317
- await client.auth.renewTokens();
4318
- console.debug("Successfully renewed tokens; retrying request.");
4319
- return requestToSend.query(queryParams);
4326
+ if (state.authReducer.isLoggedIn) {
4327
+ console.debug("Forbidden; renewing tokens and retrying.");
4328
+ await client.auth.renewTokens();
4329
+ console.debug("Successfully renewed tokens; retrying request.");
4330
+ return requestToSend.query(queryParams);
4331
+ } else {
4332
+ console.debug("Forbidden; user is not logged in.");
4333
+ throw new APIError({
4334
+ message: "Incorrect username or password.",
4335
+ response: errorResponse,
4336
+ discard: true,
4337
+ innerError: error2
4338
+ });
4339
+ }
4320
4340
  }
4321
4341
  throw new APIError({ response: errorResponse, innerError: error2, discard: discardStatuses.includes(status) });
4322
4342
  }
@@ -5138,6 +5158,7 @@ var __publicField = (obj, key, value) => {
5138
5158
  url: "/api/token/",
5139
5159
  payload: credentials,
5140
5160
  isAuthNeeded: false,
5161
+ checkAuth: false,
5141
5162
  blockers: [],
5142
5163
  blocks: []
5143
5164
  }).then(parseTokens).catch((e) => {