@marianmeres/http-utils 1.4.0 → 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/dist/error.d.ts +1 -2
- package/dist/index.cjs +3 -7
- package/dist/index.js +3 -7
- package/package.json +1 -1
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
|
-
detail: any;
|
|
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
|
-
detail = null;
|
|
112
111
|
}
|
|
113
112
|
// some more specific instances of the well known ones...
|
|
114
113
|
// client
|
|
@@ -213,10 +212,8 @@ const _wellKnownCtorMap = {
|
|
|
213
212
|
'503': ServiceUnavailable,
|
|
214
213
|
};
|
|
215
214
|
const createHttpError = (code, message,
|
|
216
|
-
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
217
|
-
detail,
|
|
218
215
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
219
|
-
// arbitrary
|
|
216
|
+
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
220
217
|
cause) => {
|
|
221
218
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
222
219
|
code = Number(code);
|
|
@@ -225,10 +222,10 @@ cause) => {
|
|
|
225
222
|
const found = HTTP_STATUS.findByCode(code);
|
|
226
223
|
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
227
224
|
// opinionated convention
|
|
228
|
-
if (typeof
|
|
225
|
+
if (typeof cause === 'string') {
|
|
229
226
|
// prettier-ignore
|
|
230
227
|
try {
|
|
231
|
-
|
|
228
|
+
cause = JSON.parse(cause);
|
|
232
229
|
}
|
|
233
230
|
catch (e) { }
|
|
234
231
|
}
|
|
@@ -237,7 +234,6 @@ cause) => {
|
|
|
237
234
|
let e = new ctor(message || statusText, { cause });
|
|
238
235
|
e.status = found?.CODE ?? fallback.CODE;
|
|
239
236
|
e.statusText = statusText;
|
|
240
|
-
e.detail = detail;
|
|
241
237
|
return e;
|
|
242
238
|
};
|
|
243
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
|
-
detail = null;
|
|
110
109
|
}
|
|
111
110
|
// some more specific instances of the well known ones...
|
|
112
111
|
// client
|
|
@@ -211,10 +210,8 @@ const _wellKnownCtorMap = {
|
|
|
211
210
|
'503': ServiceUnavailable,
|
|
212
211
|
};
|
|
213
212
|
const createHttpError = (code, message,
|
|
214
|
-
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
215
|
-
detail,
|
|
216
213
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
217
|
-
// arbitrary
|
|
214
|
+
// arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
|
|
218
215
|
cause) => {
|
|
219
216
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
220
217
|
code = Number(code);
|
|
@@ -223,10 +220,10 @@ cause) => {
|
|
|
223
220
|
const found = HTTP_STATUS.findByCode(code);
|
|
224
221
|
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
225
222
|
// opinionated convention
|
|
226
|
-
if (typeof
|
|
223
|
+
if (typeof cause === 'string') {
|
|
227
224
|
// prettier-ignore
|
|
228
225
|
try {
|
|
229
|
-
|
|
226
|
+
cause = JSON.parse(cause);
|
|
230
227
|
}
|
|
231
228
|
catch (e) { }
|
|
232
229
|
}
|
|
@@ -235,7 +232,6 @@ cause) => {
|
|
|
235
232
|
let e = new ctor(message || statusText, { cause });
|
|
236
233
|
e.status = found?.CODE ?? fallback.CODE;
|
|
237
234
|
e.statusText = statusText;
|
|
238
|
-
e.detail = detail;
|
|
239
235
|
return e;
|
|
240
236
|
};
|
|
241
237
|
|