@httptoolkit/util 0.1.2 → 0.1.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/errors.d.ts +20 -2
- package/dist/errors.js +30 -7
- package/dist/errors.js.map +1 -1
- package/package.json +1 -1
- package/src/errors.ts +44 -8
package/dist/errors.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export type ErrorLike = Partial<Error> & {
|
|
|
8
8
|
signal?: string;
|
|
9
9
|
statusCode?: number;
|
|
10
10
|
statusMessage?: string;
|
|
11
|
+
cause?: ErrorLike;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
* An easy way to check if an unknown object (e.g. a catch(e) argument) is
|
|
@@ -18,8 +19,25 @@ export declare function isErrorLike(error: any): error is ErrorLike;
|
|
|
18
19
|
* A convenience method to make something error-ish into an actual Error instance.
|
|
19
20
|
*/
|
|
20
21
|
export declare function asErrorLike(error: any): ErrorLike;
|
|
21
|
-
export declare
|
|
22
|
-
constructor(message?: string
|
|
22
|
+
export declare class CustomError extends Error implements ErrorLike {
|
|
23
|
+
constructor(message?: string, extras?: {
|
|
24
|
+
code?: string;
|
|
25
|
+
statusCode?: number;
|
|
26
|
+
cause?: Error;
|
|
27
|
+
});
|
|
28
|
+
readonly code?: string;
|
|
29
|
+
readonly statusCode?: number;
|
|
30
|
+
readonly cause?: Error;
|
|
31
|
+
}
|
|
32
|
+
export declare class StatusError extends CustomError {
|
|
33
|
+
constructor(
|
|
34
|
+
/**
|
|
35
|
+
* Should be a valid HTTP status code
|
|
36
|
+
*/
|
|
37
|
+
statusCode: number, message: string, extras?: {
|
|
38
|
+
code?: string;
|
|
39
|
+
cause?: Error;
|
|
40
|
+
});
|
|
23
41
|
}
|
|
24
42
|
/**
|
|
25
43
|
* An error to throw in expected-never cases - by using this, you ask TypeScript to
|
package/dist/errors.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unreachableCheck = exports.UnreachableCheck = exports.CustomError = exports.asErrorLike = exports.isErrorLike = void 0;
|
|
3
|
+
exports.unreachableCheck = exports.UnreachableCheck = exports.StatusError = exports.CustomError = exports.asErrorLike = exports.isErrorLike = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* An easy way to check if an unknown object (e.g. a catch(e) argument) is
|
|
6
6
|
* actually an error-like
|
|
7
7
|
*/
|
|
8
8
|
function isErrorLike(error) {
|
|
9
|
-
return typeof error === 'object' && (error instanceof Error ||
|
|
9
|
+
return error && typeof error === 'object' && (error instanceof Error ||
|
|
10
10
|
error.message ||
|
|
11
11
|
error.code ||
|
|
12
12
|
error.stack);
|
|
@@ -16,21 +16,44 @@ exports.isErrorLike = isErrorLike;
|
|
|
16
16
|
* A convenience method to make something error-ish into an actual Error instance.
|
|
17
17
|
*/
|
|
18
18
|
function asErrorLike(error) {
|
|
19
|
-
if (isErrorLike(error))
|
|
19
|
+
if (isErrorLike(error)) {
|
|
20
20
|
return error;
|
|
21
|
+
}
|
|
22
|
+
else if (error) {
|
|
23
|
+
return new Error(error.message || error.toString() || '[Unknown error]');
|
|
24
|
+
}
|
|
21
25
|
else {
|
|
22
|
-
return new Error(
|
|
26
|
+
return new Error('[Undefined error]');
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
29
|
exports.asErrorLike = asErrorLike;
|
|
26
|
-
// Tiny wrapper to make it super easy to make custom error classes where .name behaves
|
|
30
|
+
// Tiny wrapper to make it super easy to make custom error classes where .name behaves
|
|
31
|
+
// correctly, and useful metafields can be easily added.
|
|
27
32
|
class CustomError extends Error {
|
|
28
|
-
constructor(message) {
|
|
29
|
-
super(message);
|
|
33
|
+
constructor(message, extras = {}) {
|
|
34
|
+
super(message); // 'Error' breaks prototype chain here
|
|
35
|
+
// This restores the details of the real error subclass:
|
|
30
36
|
this.name = new.target.name;
|
|
37
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
38
|
+
if (extras.code !== undefined)
|
|
39
|
+
this.code = extras.code;
|
|
40
|
+
if (extras.statusCode !== undefined)
|
|
41
|
+
this.statusCode = extras.statusCode;
|
|
42
|
+
if (extras.cause !== undefined)
|
|
43
|
+
this.cause = extras.cause;
|
|
31
44
|
}
|
|
32
45
|
}
|
|
33
46
|
exports.CustomError = CustomError;
|
|
47
|
+
class StatusError extends CustomError {
|
|
48
|
+
constructor(
|
|
49
|
+
/**
|
|
50
|
+
* Should be a valid HTTP status code
|
|
51
|
+
*/
|
|
52
|
+
statusCode, message, extras = {}) {
|
|
53
|
+
super(message, { ...extras, statusCode: statusCode });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.StatusError = StatusError;
|
|
34
57
|
/**
|
|
35
58
|
* An error to throw in expected-never cases - by using this, you ask TypeScript to
|
|
36
59
|
* be sure that it agrees that the case is truly unreachable.
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAcA;;;GAGG;AACH,SAAgB,WAAW,CAAC,KAAU;IAClC,OAAO,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CACzC,KAAK,YAAY,KAAK;QACtB,KAAK,CAAC,OAAO;QACb,KAAK,CAAC,IAAI;QACV,KAAK,CAAC,KAAK,CACd,CAAA;AACL,CAAC;AAPD,kCAOC;AAED;;GAEG;AACH,SAAgB,WAAW,CAAC,KAAU;IAClC,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;QACpB,OAAO,KAAkB,CAAC;KAC7B;SAAM,IAAI,KAAK,EAAE;QACd,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,IAAI,iBAAiB,CAAC,CAAC;KAC5E;SAAM;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E,WAA4B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAS,EAAE;IAC1F,MAAM,IAAI,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAChD,CAAC,CAAA;AAFY,QAAA,gBAAgB,oBAE5B"}
|
package/package.json
CHANGED
package/src/errors.ts
CHANGED
|
@@ -9,6 +9,7 @@ export type ErrorLike = Partial<Error> & {
|
|
|
9
9
|
signal?: string;
|
|
10
10
|
statusCode?: number;
|
|
11
11
|
statusMessage?: string;
|
|
12
|
+
cause?: ErrorLike;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -16,7 +17,7 @@ export type ErrorLike = Partial<Error> & {
|
|
|
16
17
|
* actually an error-like
|
|
17
18
|
*/
|
|
18
19
|
export function isErrorLike(error: any): error is ErrorLike {
|
|
19
|
-
return typeof error === 'object' && (
|
|
20
|
+
return error && typeof error === 'object' && (
|
|
20
21
|
error instanceof Error ||
|
|
21
22
|
error.message ||
|
|
22
23
|
error.code ||
|
|
@@ -28,17 +29,52 @@ export function isErrorLike(error: any): error is ErrorLike {
|
|
|
28
29
|
* A convenience method to make something error-ish into an actual Error instance.
|
|
29
30
|
*/
|
|
30
31
|
export function asErrorLike(error: any): ErrorLike {
|
|
31
|
-
if (isErrorLike(error))
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
if (isErrorLike(error)) {
|
|
33
|
+
return error as ErrorLike;
|
|
34
|
+
} else if (error) {
|
|
35
|
+
return new Error(error.message || error.toString() || '[Unknown error]');
|
|
36
|
+
} else {
|
|
37
|
+
return new Error('[Undefined error]');
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
// Tiny wrapper to make it super easy to make custom error classes where .name behaves
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
// Tiny wrapper to make it super easy to make custom error classes where .name behaves
|
|
42
|
+
// correctly, and useful metafields can be easily added.
|
|
43
|
+
export class CustomError extends Error implements ErrorLike {
|
|
44
|
+
constructor(message?: string, extras: {
|
|
45
|
+
code?: string,
|
|
46
|
+
statusCode?: number,
|
|
47
|
+
cause?: Error
|
|
48
|
+
} = {}) {
|
|
49
|
+
super(message); // 'Error' breaks prototype chain here
|
|
50
|
+
|
|
51
|
+
// This restores the details of the real error subclass:
|
|
41
52
|
this.name = new.target.name;
|
|
53
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
54
|
+
|
|
55
|
+
if (extras.code !== undefined) this.code = extras.code;
|
|
56
|
+
if (extras.statusCode !== undefined) this.statusCode = extras.statusCode;
|
|
57
|
+
if (extras.cause !== undefined) this.cause = extras.cause;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public readonly code?: string;
|
|
61
|
+
public readonly statusCode?: number;
|
|
62
|
+
public readonly cause?: Error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class StatusError extends CustomError {
|
|
66
|
+
constructor(
|
|
67
|
+
/**
|
|
68
|
+
* Should be a valid HTTP status code
|
|
69
|
+
*/
|
|
70
|
+
statusCode: number,
|
|
71
|
+
message: string,
|
|
72
|
+
extras: {
|
|
73
|
+
code?: string,
|
|
74
|
+
cause?: Error
|
|
75
|
+
} = {}
|
|
76
|
+
) {
|
|
77
|
+
super(message, { ...extras, statusCode: statusCode });
|
|
42
78
|
}
|
|
43
79
|
}
|
|
44
80
|
|