@marianmeres/http-utils 2.6.0 → 2.7.0
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/error.d.ts +12 -4
- package/dist/error.js +36 -4
- package/package.json +1 -1
package/dist/error.d.ts
CHANGED
|
@@ -190,11 +190,19 @@ export declare const createHttpError: (code: number | string, message?: string |
|
|
|
190
190
|
*
|
|
191
191
|
* Priority order:
|
|
192
192
|
* 1. e.cause.message / e.cause.code / e.cause (if string)
|
|
193
|
-
*
|
|
193
|
+
* / e.cause.detail (RFC 7807) / e.cause.error_description (OAuth 2)
|
|
194
|
+
* / e.cause.error.error_description / e.cause.error.detail
|
|
195
|
+
* 2. e.body.error.message / e.body.message
|
|
196
|
+
* / e.body.error.error_description / e.body.error.detail
|
|
197
|
+
* / e.body.detail (RFC 7807 / DRF / FastAPI) / e.body.title (RFC 7807)
|
|
198
|
+
* / e.body.error_description (OAuth 2)
|
|
199
|
+
* / e.body.errors[0] (JSON:API; first entry's detail/title/message, or string)
|
|
200
|
+
* / e.body.error / e.body (if string)
|
|
194
201
|
* 3. e.message
|
|
195
|
-
* 4. e.
|
|
196
|
-
* 5. e.
|
|
197
|
-
* 6.
|
|
202
|
+
* 4. e.code (Node.js: ECONNREFUSED, ENOENT, ...)
|
|
203
|
+
* 5. e.name
|
|
204
|
+
* 6. e.toString()
|
|
205
|
+
* 7. "Unknown Error"
|
|
198
206
|
*
|
|
199
207
|
* @param e - The error to extract a message from (can be any type).
|
|
200
208
|
* @param stripErrorPrefix - Whether to remove "Error: " prefix from the message (default: true).
|
package/dist/error.js
CHANGED
|
@@ -250,11 +250,19 @@ export const createHttpError = (code, message, body, cause) => {
|
|
|
250
250
|
*
|
|
251
251
|
* Priority order:
|
|
252
252
|
* 1. e.cause.message / e.cause.code / e.cause (if string)
|
|
253
|
-
*
|
|
253
|
+
* / e.cause.detail (RFC 7807) / e.cause.error_description (OAuth 2)
|
|
254
|
+
* / e.cause.error.error_description / e.cause.error.detail
|
|
255
|
+
* 2. e.body.error.message / e.body.message
|
|
256
|
+
* / e.body.error.error_description / e.body.error.detail
|
|
257
|
+
* / e.body.detail (RFC 7807 / DRF / FastAPI) / e.body.title (RFC 7807)
|
|
258
|
+
* / e.body.error_description (OAuth 2)
|
|
259
|
+
* / e.body.errors[0] (JSON:API; first entry's detail/title/message, or string)
|
|
260
|
+
* / e.body.error / e.body (if string)
|
|
254
261
|
* 3. e.message
|
|
255
|
-
* 4. e.
|
|
256
|
-
* 5. e.
|
|
257
|
-
* 6.
|
|
262
|
+
* 4. e.code (Node.js: ECONNREFUSED, ENOENT, ...)
|
|
263
|
+
* 5. e.name
|
|
264
|
+
* 6. e.toString()
|
|
265
|
+
* 7. "Unknown Error"
|
|
258
266
|
*
|
|
259
267
|
* @param e - The error to extract a message from (can be any type).
|
|
260
268
|
* @param stripErrorPrefix - Whether to remove "Error: " prefix from the message (default: true).
|
|
@@ -277,13 +285,37 @@ export const getErrorMessage = (e, stripErrorPrefix = true) => {
|
|
|
277
285
|
(typeof cause === 'object' ? cause?.message : null) ||
|
|
278
286
|
(typeof cause === 'object' ? cause?.code : null) ||
|
|
279
287
|
(typeof cause === 'string' ? cause : null) ||
|
|
288
|
+
// additional well-known cause shapes (RFC 7807, OAuth 2, nested OAuth)
|
|
289
|
+
(typeof cause === 'object' ? cause?.detail : null) ||
|
|
290
|
+
(typeof cause === 'object' ? cause?.error_description : null) ||
|
|
291
|
+
(typeof cause === 'object' ? cause?.error?.error_description : null) ||
|
|
292
|
+
(typeof cause === 'object' ? cause?.error?.detail : null) ||
|
|
280
293
|
// non-standard "body" is this package's HttpError prop
|
|
281
294
|
(typeof body === 'object' ? body?.error?.message : null) ||
|
|
282
295
|
(typeof body === 'object' ? body?.message : null) ||
|
|
296
|
+
// nested under body.error (OAuth-style nested + RFC 7807 nested)
|
|
297
|
+
(typeof body === 'object' ? body?.error?.error_description : null) ||
|
|
298
|
+
(typeof body === 'object' ? body?.error?.detail : null) ||
|
|
299
|
+
// RFC 7807 (Problem Details) / DRF / FastAPI
|
|
300
|
+
(typeof body === 'object' ? body?.detail : null) ||
|
|
301
|
+
(typeof body === 'object' ? body?.title : null) ||
|
|
302
|
+
// OAuth 2 (RFC 6749) — must precede `body.error` so error_description wins over the error code
|
|
303
|
+
(typeof body === 'object' ? body?.error_description : null) ||
|
|
304
|
+
// JSON:API / generic errors[] — first entry's detail/title/message, else string
|
|
305
|
+
(typeof body === 'object' && Array.isArray(body?.errors)
|
|
306
|
+
? (typeof body.errors[0] === 'string'
|
|
307
|
+
? body.errors[0]
|
|
308
|
+
: (body.errors[0]?.detail
|
|
309
|
+
|| body.errors[0]?.title
|
|
310
|
+
|| body.errors[0]?.message))
|
|
311
|
+
: null) ||
|
|
312
|
+
// fallback: body.error as plain string (e.g. OAuth error code without description)
|
|
283
313
|
(typeof body === 'object' ? body?.error : null) ||
|
|
284
314
|
(typeof body === 'string' ? body : null) ||
|
|
285
315
|
// the common message from Error ctor (e.g. "Foo" if new TypeError("Foo"))
|
|
286
316
|
err?.message ||
|
|
317
|
+
// Node.js error code fallback (e.g. "ECONNREFUSED") when message is empty
|
|
318
|
+
err?.code ||
|
|
287
319
|
// the Error class name (e.g. TypeError)
|
|
288
320
|
err?.name ||
|
|
289
321
|
// this should handle (almost) everything else (mainly if e is not an Error instance)
|