@marianmeres/http-utils 1.3.2 → 1.5.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/README.md +1 -1
- package/dist/error.d.ts +1 -2
- package/dist/index.cjs +5 -5
- package/dist/index.js +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ try {
|
|
|
30
30
|
assert(e.toString() === 'HttpNotFoundError: Not Found');
|
|
31
31
|
assert(e.status === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE);
|
|
32
32
|
assert(e.statusText === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.TEXT);
|
|
33
|
-
assert(e.
|
|
33
|
+
assert(e.detail.message === 'hey');
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// EXAMPLE: assuming `/foo` returns 404 header and json {"message":"hey"}
|
package/dist/error.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ declare class HttpError extends Error {
|
|
|
2
2
|
name: string;
|
|
3
3
|
status: number;
|
|
4
4
|
statusText: string;
|
|
5
|
-
body: string | null;
|
|
6
5
|
}
|
|
7
6
|
declare class BadRequest extends HttpError {
|
|
8
7
|
name: string;
|
|
@@ -83,5 +82,5 @@ export declare const HTTP_ERROR: {
|
|
|
83
82
|
BadGateway: typeof BadGateway;
|
|
84
83
|
ServiceUnavailable: typeof ServiceUnavailable;
|
|
85
84
|
};
|
|
86
|
-
export declare const createHttpError: (code: number | string, message?: string | null,
|
|
85
|
+
export declare const createHttpError: (code: number | string, message?: string | null, cause?: any) => BadRequest | Unauthorized | Forbidden | NotFound | MethodNotAllowed | RequestTimeout | Conflict | Gone | ImATeapot | InternalServerError | NotImplemented | BadGateway | ServiceUnavailable;
|
|
87
86
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -108,7 +108,6 @@ class HttpError extends Error {
|
|
|
108
108
|
name = 'HttpError';
|
|
109
109
|
status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
|
|
110
110
|
statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
|
|
111
|
-
body = null;
|
|
112
111
|
}
|
|
113
112
|
// some more specific instances of the well known ones...
|
|
114
113
|
// client
|
|
@@ -212,8 +211,10 @@ const _wellKnownCtorMap = {
|
|
|
212
211
|
'502': BadGateway,
|
|
213
212
|
'503': ServiceUnavailable,
|
|
214
213
|
};
|
|
214
|
+
const createHttpError = (code, message,
|
|
215
215
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
216
|
-
|
|
216
|
+
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
217
|
+
cause) => {
|
|
217
218
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
218
219
|
code = Number(code);
|
|
219
220
|
if (isNaN(code) || !(code >= 400 && code < 600))
|
|
@@ -221,10 +222,10 @@ const createHttpError = (code, message, body, cause) => {
|
|
|
221
222
|
const found = HTTP_STATUS.findByCode(code);
|
|
222
223
|
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
223
224
|
// opinionated convention
|
|
224
|
-
if (typeof
|
|
225
|
+
if (typeof cause === 'string') {
|
|
225
226
|
// prettier-ignore
|
|
226
227
|
try {
|
|
227
|
-
|
|
228
|
+
cause = JSON.parse(cause);
|
|
228
229
|
}
|
|
229
230
|
catch (e) { }
|
|
230
231
|
}
|
|
@@ -233,7 +234,6 @@ const createHttpError = (code, message, body, cause) => {
|
|
|
233
234
|
let e = new ctor(message || statusText, { cause });
|
|
234
235
|
e.status = found?.CODE ?? fallback.CODE;
|
|
235
236
|
e.statusText = statusText;
|
|
236
|
-
e.body = body;
|
|
237
237
|
return e;
|
|
238
238
|
};
|
|
239
239
|
|
package/dist/index.js
CHANGED
|
@@ -106,7 +106,6 @@ class HttpError extends Error {
|
|
|
106
106
|
name = 'HttpError';
|
|
107
107
|
status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
|
|
108
108
|
statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
|
|
109
|
-
body = null;
|
|
110
109
|
}
|
|
111
110
|
// some more specific instances of the well known ones...
|
|
112
111
|
// client
|
|
@@ -210,8 +209,10 @@ const _wellKnownCtorMap = {
|
|
|
210
209
|
'502': BadGateway,
|
|
211
210
|
'503': ServiceUnavailable,
|
|
212
211
|
};
|
|
212
|
+
const createHttpError = (code, message,
|
|
213
213
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
214
|
-
|
|
214
|
+
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
215
|
+
cause) => {
|
|
215
216
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
216
217
|
code = Number(code);
|
|
217
218
|
if (isNaN(code) || !(code >= 400 && code < 600))
|
|
@@ -219,10 +220,10 @@ const createHttpError = (code, message, body, cause) => {
|
|
|
219
220
|
const found = HTTP_STATUS.findByCode(code);
|
|
220
221
|
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
221
222
|
// opinionated convention
|
|
222
|
-
if (typeof
|
|
223
|
+
if (typeof cause === 'string') {
|
|
223
224
|
// prettier-ignore
|
|
224
225
|
try {
|
|
225
|
-
|
|
226
|
+
cause = JSON.parse(cause);
|
|
226
227
|
}
|
|
227
228
|
catch (e) { }
|
|
228
229
|
}
|
|
@@ -231,7 +232,6 @@ const createHttpError = (code, message, body, cause) => {
|
|
|
231
232
|
let e = new ctor(message || statusText, { cause });
|
|
232
233
|
e.status = found?.CODE ?? fallback.CODE;
|
|
233
234
|
e.statusText = statusText;
|
|
234
|
-
e.body = body;
|
|
235
235
|
return e;
|
|
236
236
|
};
|
|
237
237
|
|