@polygonlabs/verror 0.0.0-0 → 0.2.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/http.d.ts +85 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +89 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/verror.d.ts +104 -0
- package/dist/verror.d.ts.map +1 -0
- package/dist/verror.js +255 -0
- package/dist/verror.js.map +1 -0
- package/package.json +9 -2
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { VError } from './verror.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for all HTTP-specific errors. Provides `statusCode` with `toJSON()`
|
|
4
|
+
*
|
|
5
|
+
* Extend this class (or any concrete subclass) to create domain-specific HTTP
|
|
6
|
+
* error types:
|
|
7
|
+
*
|
|
8
|
+
* class ResourceNotFound extends NotFound {
|
|
9
|
+
* override readonly name = 'ResourceNotFound' as const;
|
|
10
|
+
* }
|
|
11
|
+
*/
|
|
12
|
+
export declare class HTTPError extends VError {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly statusCode: number;
|
|
15
|
+
toJSON(): Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
export declare class BadRequest extends HTTPError {
|
|
18
|
+
readonly name: "BadRequest";
|
|
19
|
+
readonly statusCode: 400;
|
|
20
|
+
}
|
|
21
|
+
export declare class NotAuthenticated extends HTTPError {
|
|
22
|
+
readonly name: "NotAuthenticated";
|
|
23
|
+
readonly statusCode: 401;
|
|
24
|
+
}
|
|
25
|
+
export declare class PaymentError extends HTTPError {
|
|
26
|
+
readonly name: "PaymentError";
|
|
27
|
+
readonly statusCode: 402;
|
|
28
|
+
}
|
|
29
|
+
export declare class Forbidden extends HTTPError {
|
|
30
|
+
readonly name: "Forbidden";
|
|
31
|
+
readonly statusCode: 403;
|
|
32
|
+
}
|
|
33
|
+
export declare class NotFound extends HTTPError {
|
|
34
|
+
readonly name: "NotFound";
|
|
35
|
+
readonly statusCode: 404;
|
|
36
|
+
}
|
|
37
|
+
export declare class MethodNotAllowed extends HTTPError {
|
|
38
|
+
readonly name: "MethodNotAllowed";
|
|
39
|
+
readonly statusCode: 405;
|
|
40
|
+
}
|
|
41
|
+
export declare class NotAcceptable extends HTTPError {
|
|
42
|
+
readonly name: "NotAcceptable";
|
|
43
|
+
readonly statusCode: 406;
|
|
44
|
+
}
|
|
45
|
+
export declare class Timeout extends HTTPError {
|
|
46
|
+
readonly name: "Timeout";
|
|
47
|
+
readonly statusCode: 408;
|
|
48
|
+
}
|
|
49
|
+
export declare class Conflict extends HTTPError {
|
|
50
|
+
readonly name: "Conflict";
|
|
51
|
+
readonly statusCode: 409;
|
|
52
|
+
}
|
|
53
|
+
export declare class Gone extends HTTPError {
|
|
54
|
+
readonly name: "Gone";
|
|
55
|
+
readonly statusCode: 410;
|
|
56
|
+
}
|
|
57
|
+
export declare class LengthRequired extends HTTPError {
|
|
58
|
+
readonly name: "LengthRequired";
|
|
59
|
+
readonly statusCode: 411;
|
|
60
|
+
}
|
|
61
|
+
export declare class Unprocessable extends HTTPError {
|
|
62
|
+
readonly name: "Unprocessable";
|
|
63
|
+
readonly statusCode: 422;
|
|
64
|
+
}
|
|
65
|
+
export declare class TooManyRequests extends HTTPError {
|
|
66
|
+
readonly name: "TooManyRequests";
|
|
67
|
+
readonly statusCode: 429;
|
|
68
|
+
}
|
|
69
|
+
export declare class GeneralError extends HTTPError {
|
|
70
|
+
readonly name: "GeneralError";
|
|
71
|
+
readonly statusCode: 500;
|
|
72
|
+
}
|
|
73
|
+
export declare class NotImplemented extends HTTPError {
|
|
74
|
+
readonly name: "NotImplemented";
|
|
75
|
+
readonly statusCode: 501;
|
|
76
|
+
}
|
|
77
|
+
export declare class BadGateway extends HTTPError {
|
|
78
|
+
readonly name: "BadGateway";
|
|
79
|
+
readonly statusCode: 502;
|
|
80
|
+
}
|
|
81
|
+
export declare class Unavailable extends HTTPError {
|
|
82
|
+
readonly name: "Unavailable";
|
|
83
|
+
readonly statusCode: 503;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;GASG;AACH,qBAAa,SAAU,SAAQ,MAAM;IACnC,SAAkB,IAAI,EAAE,MAAM,CAAe;IAC7C,SAAiB,UAAU,EAAE,MAAM,CAAC;IAE3B,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAM3C;AAED,qBAAa,UAAW,SAAQ,SAAS;IACvC,SAAkB,IAAI,EAAG,YAAY,CAAU;IAC/C,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C;AAED,qBAAa,gBAAiB,SAAQ,SAAS;IAC7C,SAAkB,IAAI,EAAG,kBAAkB,CAAU;IACrD,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C;AAED,qBAAa,YAAa,SAAQ,SAAS;IACzC,SAAkB,IAAI,EAAG,cAAc,CAAU;IACjD,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C;AAED,qBAAa,SAAU,SAAQ,SAAS;IACtC,SAAkB,IAAI,EAAG,WAAW,CAAU;IAC9C,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C;AAED,qBAAa,QAAS,SAAQ,SAAS;IACrC,SAAkB,IAAI,EAAG,UAAU,CAAU;IAC7C,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C;AAED,qBAAa,gBAAiB,SAAQ,SAAS;IAC7C,SAAkB,IAAI,EA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AAQ,SAAS;IACxC,SAAkB,IAAI,EAAG,aAAa,CAAU;IAChD,SAAkB,UAAU,EAAG,GAAG,CAAU;CAC7C"}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { VError } from "./verror.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for all HTTP-specific errors. Provides `statusCode` with `toJSON()`
|
|
4
|
+
*
|
|
5
|
+
* Extend this class (or any concrete subclass) to create domain-specific HTTP
|
|
6
|
+
* error types:
|
|
7
|
+
*
|
|
8
|
+
* class ResourceNotFound extends NotFound {
|
|
9
|
+
* override readonly name = 'ResourceNotFound' as const;
|
|
10
|
+
* }
|
|
11
|
+
*/
|
|
12
|
+
export class HTTPError extends VError {
|
|
13
|
+
name = 'HTTPError';
|
|
14
|
+
toJSON() {
|
|
15
|
+
return {
|
|
16
|
+
...super.toJSON(),
|
|
17
|
+
statusCode: this.statusCode
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class BadRequest extends HTTPError {
|
|
22
|
+
name = 'BadRequest';
|
|
23
|
+
statusCode = 400;
|
|
24
|
+
}
|
|
25
|
+
export class NotAuthenticated extends HTTPError {
|
|
26
|
+
name = 'NotAuthenticated';
|
|
27
|
+
statusCode = 401;
|
|
28
|
+
}
|
|
29
|
+
export class PaymentError extends HTTPError {
|
|
30
|
+
name = 'PaymentError';
|
|
31
|
+
statusCode = 402;
|
|
32
|
+
}
|
|
33
|
+
export class Forbidden extends HTTPError {
|
|
34
|
+
name = 'Forbidden';
|
|
35
|
+
statusCode = 403;
|
|
36
|
+
}
|
|
37
|
+
export class NotFound extends HTTPError {
|
|
38
|
+
name = 'NotFound';
|
|
39
|
+
statusCode = 404;
|
|
40
|
+
}
|
|
41
|
+
export class MethodNotAllowed extends HTTPError {
|
|
42
|
+
name = 'MethodNotAllowed';
|
|
43
|
+
statusCode = 405;
|
|
44
|
+
}
|
|
45
|
+
export class NotAcceptable extends HTTPError {
|
|
46
|
+
name = 'NotAcceptable';
|
|
47
|
+
statusCode = 406;
|
|
48
|
+
}
|
|
49
|
+
export class Timeout extends HTTPError {
|
|
50
|
+
name = 'Timeout';
|
|
51
|
+
statusCode = 408;
|
|
52
|
+
}
|
|
53
|
+
export class Conflict extends HTTPError {
|
|
54
|
+
name = 'Conflict';
|
|
55
|
+
statusCode = 409;
|
|
56
|
+
}
|
|
57
|
+
export class Gone extends HTTPError {
|
|
58
|
+
name = 'Gone';
|
|
59
|
+
statusCode = 410;
|
|
60
|
+
}
|
|
61
|
+
export class LengthRequired extends HTTPError {
|
|
62
|
+
name = 'LengthRequired';
|
|
63
|
+
statusCode = 411;
|
|
64
|
+
}
|
|
65
|
+
export class Unprocessable extends HTTPError {
|
|
66
|
+
name = 'Unprocessable';
|
|
67
|
+
statusCode = 422;
|
|
68
|
+
}
|
|
69
|
+
export class TooManyRequests extends HTTPError {
|
|
70
|
+
name = 'TooManyRequests';
|
|
71
|
+
statusCode = 429;
|
|
72
|
+
}
|
|
73
|
+
export class GeneralError extends HTTPError {
|
|
74
|
+
name = 'GeneralError';
|
|
75
|
+
statusCode = 500;
|
|
76
|
+
}
|
|
77
|
+
export class NotImplemented extends HTTPError {
|
|
78
|
+
name = 'NotImplemented';
|
|
79
|
+
statusCode = 501;
|
|
80
|
+
}
|
|
81
|
+
export class BadGateway extends HTTPError {
|
|
82
|
+
name = 'BadGateway';
|
|
83
|
+
statusCode = 502;
|
|
84
|
+
}
|
|
85
|
+
export class Unavailable extends HTTPError {
|
|
86
|
+
name = 'Unavailable';
|
|
87
|
+
statusCode = 503;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;GASG;AACH,MAAM,OAAO,S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7C;AAED,MAAM,OAAO,cAAe,SAAQ,SAAS;IACzB,IAAI,GAAG,gBAAyB,CAAC;IACjC,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,aAAc,SAAQ,SAAS;IACxB,IAAI,GAAG,eAAwB,CAAC;IAChC,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC1B,IAAI,GAAG,iBAA0B,CAAC;IAClC,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,YAAa,SAAQ,SAAS;IACvB,IAAI,GAAG,cAAuB,CAAC;IAC/B,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,cAAe,SAAQ,SAAS;IACzB,IAAI,GAAG,gBAAyB,CAAC;IACjC,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,UAAW,SAAQ,SAAS;IACrB,IAAI,GAAG,YAAqB,CAAC;IAC7B,UAAU,GAAG,GAAY,CAAC;CAC7C;AAED,MAAM,OAAO,WAAY,SAAQ,SAAS;IACtB,IAAI,GAAG,aAAsB,CAAC;IAC9B,UAAU,GAAG,GAAY,CAAC;CAC7C"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { serializeError, VError, WError, MultiError } from './verror.ts';
|
|
2
|
+
export type { VErrorOptions } from './types.ts';
|
|
3
|
+
export { HTTPError, BadRequest, NotAuthenticated, PaymentError, Forbidden, NotFound, MethodNotAllowed, NotAcceptable, Timeout, Conflict, Gone, LengthRequired, Unprocessable, TooManyRequests, GeneralError, NotImplemented, BadGateway, Unavailable } from './http.ts';
|
|
4
|
+
export declare const cause: (err: Error) => Error | null;
|
|
5
|
+
export declare const info: (err: Error) => Record<string, unknown>;
|
|
6
|
+
export declare const fullStack: (err: Error) => string;
|
|
7
|
+
export declare const errorFromList: (errors: Error[]) => Error | null;
|
|
8
|
+
export declare const errorForEach: (err: Error, func: (e: Error) => void) => void;
|
|
9
|
+
export declare const findCauseByName: (err: Error, name: string) => Error | null;
|
|
10
|
+
export declare const findCauseByType: <T extends Error>(err: Error, type: {
|
|
11
|
+
prototype: T;
|
|
12
|
+
}) => T | null;
|
|
13
|
+
export declare const hasCauseWithName: (err: Error, name: string) => boolean;
|
|
14
|
+
export declare const hasCauseWithType: <T extends Error>(err: Error, type: {
|
|
15
|
+
prototype: T;
|
|
16
|
+
}) => boolean;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EACL,SAAS,EACT,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACT,QAAQ,EACR,gBAAgB,EAChB,aAAa,EACb,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,cAAc,EACd,aAAa,EACb,eAAe,EACf,YAAY,EACZ,cAAc,EACd,UAAU,EACV,WAAW,EACZ,MAAM,WAAW,CAAC;AAOnB,eAAO,MAAM,KAAK,GAAI,KAAK,KAAK,KAAG,KAAK,GAAG,IAAyB,CAAC;AACrE,eAAO,MAAM,IAAI,GAAI,KAAK,KAAK,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAqB,CAAC;AAC9E,eAAO,MAAM,SAAS,GAAI,KAAK,KAAK,KAAG,MAA+B,CAAC;AACvE,eAAO,MAAM,aAAa,GAAI,QAAQ,KAAK,EAAE,KAAG,KAAK,GAAG,IAAoC,CAAC;AAC7F,eAAO,MAAM,YAAY,GAAI,KAAK,KAAK,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,KAAG,IACpC,CAAC;AACjC,eAAO,MAAM,eAAe,GAAI,KAAK,KAAK,EAAE,MAAM,MAAM,KAAG,KAAK,GAAG,IAChC,CAAC;AACpC,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM;IAAE,SAAS,EAAE,CAAC,CAAA;CAAE,KAAG,CAAC,GAAG,IACvD,CAAC;AACpC,eAAO,MAAM,gBAAgB,GAAI,KAAK,KAAK,EAAE,MAAM,MAAM,KAAG,OACxB,CAAC;AACrC,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM;IAAE,SAAS,EAAE,CAAC,CAAA;CAAE,KAAG,OACnD,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { serializeError, VError, WError, MultiError } from "./verror.js";
|
|
2
|
+
export { HTTPError, BadRequest, NotAuthenticated, PaymentError, Forbidden, NotFound, MethodNotAllowed, NotAcceptable, Timeout, Conflict, Gone, LengthRequired, Unprocessable, TooManyRequests, GeneralError, NotImplemented, BadGateway, Unavailable } from "./http.js";
|
|
3
|
+
// Standalone named exports for the VError static helpers so consumers can
|
|
4
|
+
// import them without going through the class:
|
|
5
|
+
// import { findCauseByName, cause } from '@polygonlabs/verror'
|
|
6
|
+
import { VError } from "./verror.js";
|
|
7
|
+
export const cause = (err) => VError.cause(err);
|
|
8
|
+
export const info = (err) => VError.info(err);
|
|
9
|
+
export const fullStack = (err) => VError.fullStack(err);
|
|
10
|
+
export const errorFromList = (errors) => VError.errorFromList(errors);
|
|
11
|
+
export const errorForEach = (err, func) => VError.errorForEach(err, func);
|
|
12
|
+
export const findCauseByName = (err, name) => VError.findCauseByName(err, name);
|
|
13
|
+
export const findCauseByType = (err, type) => VError.findCauseByType(err, type);
|
|
14
|
+
export const hasCauseWithName = (err, name) => VError.hasCauseWithName(err, name);
|
|
15
|
+
export const hasCauseWithType = (err, type) => VError.hasCauseWithType(err, type);
|
|
16
|
+
//# source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,CAAC;AACpC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAkB,GAAU,EAAE,IAAsB,EAAY,EAAE,CAC/F,MAAM,CAAC,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AACpC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,GAAU,EAAE,IAAY,EAAW,EAAE,CACpE,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AACrC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAkB,GAAU,EAAE,IAAsB,EAAW,EAAE,CAC/F,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface VErrorOptions {
|
|
2
|
+
cause?: Error;
|
|
3
|
+
info?: Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* The function to treat as the top of the stack trace for
|
|
6
|
+
* `Error.captureStackTrace`. Frames at or below this function are omitted.
|
|
7
|
+
* Defaults to the constructor of the class being instantiated.
|
|
8
|
+
*/
|
|
9
|
+
constructorOpt?: ((...args: unknown[]) => unknown) | null;
|
|
10
|
+
/**
|
|
11
|
+
* @internal Used by WError to suppress cause message appending.
|
|
12
|
+
* Do not set this directly — use WError instead.
|
|
13
|
+
*/
|
|
14
|
+
skipCauseMessage?: boolean;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B;;;;OAIG;IACH,cAAc,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,IAAI,CAAC;IAC1D;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/verror.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { VErrorOptions } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Serialises any Error to the canonical VError JSON shape so that the full
|
|
4
|
+
* cause chain is preserved when errors are transmitted over RPC or stored in
|
|
5
|
+
* logs. Plain `Error` instances (which would otherwise serialise to `{}`)
|
|
6
|
+
* receive the same structure as VError: `name`, `message`, `shortMessage`,
|
|
7
|
+
* `cause`, `info`.
|
|
8
|
+
*
|
|
9
|
+
* Called automatically by `VError.toJSON()`. Export it if you need to
|
|
10
|
+
* serialise errors that are not necessarily VErrors themselves.
|
|
11
|
+
*/
|
|
12
|
+
export declare function serializeError(err: unknown): Record<string, unknown> | undefined;
|
|
13
|
+
export declare class VError extends Error {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the fully accumulated message for `cause`, walking native Error
|
|
17
|
+
* cause chains so VError produces a complete message even when some links
|
|
18
|
+
* are plain Errors that did not append their own cause message.
|
|
19
|
+
*
|
|
20
|
+
* VError instances already carry the full accumulated chain in `.message`
|
|
21
|
+
* and are returned directly. WError is a VError whose `.message` is its
|
|
22
|
+
* own message only (by design), so it naturally stops the accumulation.
|
|
23
|
+
*/
|
|
24
|
+
private static accumulateCauseMessage;
|
|
25
|
+
/** The message as passed to the constructor, before the cause chain was appended. */
|
|
26
|
+
readonly shortMessage: string;
|
|
27
|
+
/**
|
|
28
|
+
* Structured informational properties attached at construction time.
|
|
29
|
+
* Use `VError.info(err)` to get the merged set from the full cause chain.
|
|
30
|
+
*/
|
|
31
|
+
readonly info: Record<string, unknown>;
|
|
32
|
+
constructor(message: string, options?: VErrorOptions);
|
|
33
|
+
toString(): string;
|
|
34
|
+
toJSON(): Record<string, unknown>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the immediate cause of `err`, or null. Unlike reading `err.cause`
|
|
37
|
+
* directly (typed as `unknown` on the base Error class), this validates that
|
|
38
|
+
* the cause is an actual Error and returns a typed result.
|
|
39
|
+
*/
|
|
40
|
+
static cause(err: Error): Error | null;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the merged info object for the entire cause chain of `err`.
|
|
43
|
+
* Deeper causes appear first; closer causes override their values.
|
|
44
|
+
*/
|
|
45
|
+
static info(err: Error): Record<string, unknown>;
|
|
46
|
+
/** Walk the cause chain and return the first Error whose `name` matches. */
|
|
47
|
+
static findCauseByName(err: Error, name: string): Error | null;
|
|
48
|
+
/**
|
|
49
|
+
* Walk the cause chain and return the first Error that is `instanceof type`.
|
|
50
|
+
*
|
|
51
|
+
* The constraint uses `{ prototype: T }` rather than a constructor signature
|
|
52
|
+
* because TypeScript types built-in constructors (e.g. `Error`) with specific
|
|
53
|
+
* parameter types like `(message?: string)`, which are not compatible with a
|
|
54
|
+
* fully generic `new (...args: unknown[]) => T` form. `instanceof` only
|
|
55
|
+
* inspects the prototype chain, so `{ prototype: T }` is the correct shape.
|
|
56
|
+
*/
|
|
57
|
+
static findCauseByType<T extends Error>(err: Error, type: {
|
|
58
|
+
prototype: T;
|
|
59
|
+
}): T | null;
|
|
60
|
+
static hasCauseWithName(err: Error, name: string): boolean;
|
|
61
|
+
static hasCauseWithType<T extends Error>(err: Error, type: {
|
|
62
|
+
prototype: T;
|
|
63
|
+
}): boolean;
|
|
64
|
+
/** Concatenates the full stack trace including all chained causes. */
|
|
65
|
+
static fullStack(err: Error): string;
|
|
66
|
+
/**
|
|
67
|
+
* Given an array of errors, returns null (empty), the single error (one
|
|
68
|
+
* item), or a new `MultiError` wrapping all of them.
|
|
69
|
+
*/
|
|
70
|
+
static errorFromList(errors: Error[]): Error | null;
|
|
71
|
+
/**
|
|
72
|
+
* Calls `func` once for each contained error: once for a plain Error,
|
|
73
|
+
* once per element for a MultiError.
|
|
74
|
+
*/
|
|
75
|
+
static errorForEach(err: Error, func: (e: Error) => void): void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A "wrapped error" — like VError but the cause's message is intentionally
|
|
79
|
+
* NOT appended to this error's own message. The cause is still traversable
|
|
80
|
+
* via `VError.cause()` and visible in `toString()`, but cause chains remain
|
|
81
|
+
* separate log entries rather than a single ever-growing message string.
|
|
82
|
+
*
|
|
83
|
+
* Use WError when you want to wrap a lower-level error with a distinct
|
|
84
|
+
* higher-level description without the noise of duplicating the cause message.
|
|
85
|
+
*/
|
|
86
|
+
export declare class WError extends VError {
|
|
87
|
+
readonly name: string;
|
|
88
|
+
constructor(message: string, options: Omit<VErrorOptions, 'skipCauseMessage'> & {
|
|
89
|
+
cause: Error;
|
|
90
|
+
});
|
|
91
|
+
toString(): string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Represents a collection of errors for callers that normally deal with a
|
|
95
|
+
* single error but need to surface multiple failures. The first error in the
|
|
96
|
+
* list is used as the cause; all errors are accessible via `err.errors`.
|
|
97
|
+
*/
|
|
98
|
+
export declare class MultiError extends VError {
|
|
99
|
+
readonly name: string;
|
|
100
|
+
readonly errors: readonly Error[];
|
|
101
|
+
constructor(errors: Error[]);
|
|
102
|
+
toJSON(): Record<string, unknown>;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=verror.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verror.d.ts","sourceRoot":"","sources":["../src/verror.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,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GAAG,MAAM;IAOpC;;;OAGG;IACH,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG,IAAI;IASnD;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,GAAG,IAAI;CAShE;AAID;;;;;;;;GAQG;AACH,qBAAa,MAAO,SAAQ,MAAM;IAChC,SAAkB,IAAI,EAAE,MAAM,CAAY;gBAGxC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,kBAAkB,CAAC,GAAG;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE;IAK5D,QAAQ,IAAI,MAAM;CAY5B;AAID;;;;GAIG;AACH,qBAAa,UAAW,SAAQ,MAAM;IACpC,SAAkB,IAAI,EAAE,MAAM,CAAgB;IAC9C,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAC;gBAEtB,MAAM,EAAE,KAAK,EAAE;IAWlB,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAG3C"}
|
package/dist/verror.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialises any Error to the canonical VError JSON shape so that the full
|
|
3
|
+
* cause chain is preserved when errors are transmitted over RPC or stored in
|
|
4
|
+
* logs. Plain `Error` instances (which would otherwise serialise to `{}`)
|
|
5
|
+
* receive the same structure as VError: `name`, `message`, `shortMessage`,
|
|
6
|
+
* `cause`, `info`.
|
|
7
|
+
*
|
|
8
|
+
* Called automatically by `VError.toJSON()`. Export it if you need to
|
|
9
|
+
* serialise errors that are not necessarily VErrors themselves.
|
|
10
|
+
*/
|
|
11
|
+
export function serializeError(err) {
|
|
12
|
+
if (!(err instanceof Error))
|
|
13
|
+
return undefined;
|
|
14
|
+
// VError (and subclasses like HTTPError) already have toJSON — delegate so
|
|
15
|
+
// that subclass-specific fields (statusCode, errors, etc.) are included.
|
|
16
|
+
if (typeof err.toJSON === 'function') {
|
|
17
|
+
return err.toJSON();
|
|
18
|
+
}
|
|
19
|
+
// Plain Error: produce the same shape so the receiver can handle it uniformly.
|
|
20
|
+
return {
|
|
21
|
+
name: err.name,
|
|
22
|
+
message: err.message,
|
|
23
|
+
shortMessage: err.message,
|
|
24
|
+
cause: serializeError(err.cause),
|
|
25
|
+
info: {}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export class VError extends Error {
|
|
29
|
+
// Explicit `: string` so subclasses can narrow with `as const` to their own literal.
|
|
30
|
+
name = 'VError';
|
|
31
|
+
/**
|
|
32
|
+
* Returns the fully accumulated message for `cause`, walking native Error
|
|
33
|
+
* cause chains so VError produces a complete message even when some links
|
|
34
|
+
* are plain Errors that did not append their own cause message.
|
|
35
|
+
*
|
|
36
|
+
* VError instances already carry the full accumulated chain in `.message`
|
|
37
|
+
* and are returned directly. WError is a VError whose `.message` is its
|
|
38
|
+
* own message only (by design), so it naturally stops the accumulation.
|
|
39
|
+
*/
|
|
40
|
+
static accumulateCauseMessage(cause) {
|
|
41
|
+
if (cause instanceof VError)
|
|
42
|
+
return cause.message;
|
|
43
|
+
const nested = cause.cause;
|
|
44
|
+
if (nested instanceof Error && nested.message) {
|
|
45
|
+
const nestedMsg = VError.accumulateCauseMessage(nested);
|
|
46
|
+
return nestedMsg ? `${cause.message}: ${nestedMsg}` : cause.message;
|
|
47
|
+
}
|
|
48
|
+
return cause.message;
|
|
49
|
+
}
|
|
50
|
+
/** The message as passed to the constructor, before the cause chain was appended. */
|
|
51
|
+
shortMessage;
|
|
52
|
+
/**
|
|
53
|
+
* Structured informational properties attached at construction time.
|
|
54
|
+
* Use `VError.info(err)` to get the merged set from the full cause chain.
|
|
55
|
+
*/
|
|
56
|
+
info;
|
|
57
|
+
constructor(message, options) {
|
|
58
|
+
const { cause, constructorOpt, info: infoOpt = {}, skipCauseMessage } = options ?? {};
|
|
59
|
+
let fullMessage = message;
|
|
60
|
+
if (cause != null && !skipCauseMessage) {
|
|
61
|
+
const causeMsg = VError.accumulateCauseMessage(cause);
|
|
62
|
+
if (causeMsg)
|
|
63
|
+
fullMessage = message === '' ? causeMsg : `${message}: ${causeMsg}`;
|
|
64
|
+
}
|
|
65
|
+
super(fullMessage);
|
|
66
|
+
this.message = fullMessage;
|
|
67
|
+
this.shortMessage = message;
|
|
68
|
+
if (cause != null) {
|
|
69
|
+
Object.defineProperty(this, 'cause', {
|
|
70
|
+
value: cause,
|
|
71
|
+
writable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
enumerable: false
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
this.info = {};
|
|
77
|
+
for (const k of Object.keys(infoOpt))
|
|
78
|
+
this.info[k] = infoOpt[k];
|
|
79
|
+
if (Error.captureStackTrace) {
|
|
80
|
+
Error.captureStackTrace(this, constructorOpt ?? new.target);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
toString() {
|
|
84
|
+
const name = (Object.prototype.hasOwnProperty.call(this, 'name') && this.name) ||
|
|
85
|
+
this.constructor.name ||
|
|
86
|
+
this.constructor.prototype.name;
|
|
87
|
+
return this.message ? `${name}: ${this.message}` : name;
|
|
88
|
+
}
|
|
89
|
+
toJSON() {
|
|
90
|
+
return {
|
|
91
|
+
name: this.name,
|
|
92
|
+
message: this.message,
|
|
93
|
+
shortMessage: this.shortMessage,
|
|
94
|
+
cause: serializeError(this.cause),
|
|
95
|
+
info: this.info
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// ─── Static helpers ────────────────────────────────────────────────────────
|
|
99
|
+
/**
|
|
100
|
+
* Returns the immediate cause of `err`, or null. Unlike reading `err.cause`
|
|
101
|
+
* directly (typed as `unknown` on the base Error class), this validates that
|
|
102
|
+
* the cause is an actual Error and returns a typed result.
|
|
103
|
+
*/
|
|
104
|
+
static cause(err) {
|
|
105
|
+
if (!(err instanceof Error))
|
|
106
|
+
throw new Error('err must be an Error');
|
|
107
|
+
const c = err.cause;
|
|
108
|
+
return c instanceof Error ? c : null;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns the merged info object for the entire cause chain of `err`.
|
|
112
|
+
* Deeper causes appear first; closer causes override their values.
|
|
113
|
+
*/
|
|
114
|
+
static info(err) {
|
|
115
|
+
if (!(err instanceof Error))
|
|
116
|
+
throw new Error('err must be an Error');
|
|
117
|
+
const cause = VError.cause(err);
|
|
118
|
+
const rv = cause !== null ? VError.info(cause) : {};
|
|
119
|
+
const errInfo = err.info;
|
|
120
|
+
if (errInfo !== null && typeof errInfo === 'object')
|
|
121
|
+
Object.assign(rv, errInfo);
|
|
122
|
+
return rv;
|
|
123
|
+
}
|
|
124
|
+
/** Walk the cause chain and return the first Error whose `name` matches. */
|
|
125
|
+
static findCauseByName(err, name) {
|
|
126
|
+
if (!(err instanceof Error))
|
|
127
|
+
throw new Error('err must be an Error');
|
|
128
|
+
if (typeof name !== 'string')
|
|
129
|
+
throw new Error('name (string) is required');
|
|
130
|
+
if (name.length === 0)
|
|
131
|
+
throw new Error('name cannot be empty');
|
|
132
|
+
for (let cause = err; cause !== null; cause = VError.cause(cause)) {
|
|
133
|
+
if (cause.name === name)
|
|
134
|
+
return cause;
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Walk the cause chain and return the first Error that is `instanceof type`.
|
|
140
|
+
*
|
|
141
|
+
* The constraint uses `{ prototype: T }` rather than a constructor signature
|
|
142
|
+
* because TypeScript types built-in constructors (e.g. `Error`) with specific
|
|
143
|
+
* parameter types like `(message?: string)`, which are not compatible with a
|
|
144
|
+
* fully generic `new (...args: unknown[]) => T` form. `instanceof` only
|
|
145
|
+
* inspects the prototype chain, so `{ prototype: T }` is the correct shape.
|
|
146
|
+
*/
|
|
147
|
+
static findCauseByType(err, type) {
|
|
148
|
+
if (!(err instanceof Error))
|
|
149
|
+
throw new Error('err must be an Error');
|
|
150
|
+
if (typeof type !== 'function')
|
|
151
|
+
throw new Error('type (func) is required');
|
|
152
|
+
for (let cause = err; cause !== null; cause = VError.cause(cause)) {
|
|
153
|
+
if (cause instanceof type)
|
|
154
|
+
return cause;
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
static hasCauseWithName(err, name) {
|
|
159
|
+
return VError.findCauseByName(err, name) !== null;
|
|
160
|
+
}
|
|
161
|
+
static hasCauseWithType(err, type) {
|
|
162
|
+
return VError.findCauseByType(err, type) !== null;
|
|
163
|
+
}
|
|
164
|
+
/** Concatenates the full stack trace including all chained causes. */
|
|
165
|
+
static fullStack(err) {
|
|
166
|
+
if (!(err instanceof Error))
|
|
167
|
+
throw new Error('err must be an Error');
|
|
168
|
+
const cause = VError.cause(err);
|
|
169
|
+
const stack = err.stack ?? '';
|
|
170
|
+
return cause !== null ? `${stack}\ncaused by: ${VError.fullStack(cause)}` : stack;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Given an array of errors, returns null (empty), the single error (one
|
|
174
|
+
* item), or a new `MultiError` wrapping all of them.
|
|
175
|
+
*/
|
|
176
|
+
static errorFromList(errors) {
|
|
177
|
+
if (!Array.isArray(errors))
|
|
178
|
+
throw new Error('list of errors (array) is required');
|
|
179
|
+
if (errors.length === 0)
|
|
180
|
+
return null;
|
|
181
|
+
for (const e of errors) {
|
|
182
|
+
if (!(e instanceof Error))
|
|
183
|
+
throw new Error('each item must be an Error');
|
|
184
|
+
}
|
|
185
|
+
return errors.length === 1 ? errors[0] : new MultiError(errors);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Calls `func` once for each contained error: once for a plain Error,
|
|
189
|
+
* once per element for a MultiError.
|
|
190
|
+
*/
|
|
191
|
+
static errorForEach(err, func) {
|
|
192
|
+
if (!(err instanceof Error))
|
|
193
|
+
throw new Error('err must be an Error');
|
|
194
|
+
if (typeof func !== 'function')
|
|
195
|
+
throw new Error('func (func) is required');
|
|
196
|
+
if (err instanceof MultiError) {
|
|
197
|
+
for (const e of err.errors)
|
|
198
|
+
func(e);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
func(err);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// ─── WError ────────────────────────────────────────────────────────────────────
|
|
206
|
+
/**
|
|
207
|
+
* A "wrapped error" — like VError but the cause's message is intentionally
|
|
208
|
+
* NOT appended to this error's own message. The cause is still traversable
|
|
209
|
+
* via `VError.cause()` and visible in `toString()`, but cause chains remain
|
|
210
|
+
* separate log entries rather than a single ever-growing message string.
|
|
211
|
+
*
|
|
212
|
+
* Use WError when you want to wrap a lower-level error with a distinct
|
|
213
|
+
* higher-level description without the noise of duplicating the cause message.
|
|
214
|
+
*/
|
|
215
|
+
export class WError extends VError {
|
|
216
|
+
name = 'WError';
|
|
217
|
+
constructor(message, options) {
|
|
218
|
+
super(message, { ...options, skipCauseMessage: true });
|
|
219
|
+
}
|
|
220
|
+
toString() {
|
|
221
|
+
const name = (Object.prototype.hasOwnProperty.call(this, 'name') && this.name) ||
|
|
222
|
+
this.constructor.name ||
|
|
223
|
+
this.constructor.prototype.name;
|
|
224
|
+
let str = this.message ? `${name}: ${this.message}` : name;
|
|
225
|
+
const cause = this.cause;
|
|
226
|
+
if (cause instanceof Error && cause.message) {
|
|
227
|
+
str += `; caused by ${cause.toString()}`;
|
|
228
|
+
}
|
|
229
|
+
return str;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// ─── MultiError ────────────────────────────────────────────────────────────────
|
|
233
|
+
/**
|
|
234
|
+
* Represents a collection of errors for callers that normally deal with a
|
|
235
|
+
* single error but need to surface multiple failures. The first error in the
|
|
236
|
+
* list is used as the cause; all errors are accessible via `err.errors`.
|
|
237
|
+
*/
|
|
238
|
+
export class MultiError extends VError {
|
|
239
|
+
name = 'MultiError';
|
|
240
|
+
errors;
|
|
241
|
+
constructor(errors) {
|
|
242
|
+
if (!Array.isArray(errors))
|
|
243
|
+
throw new Error('list of errors (array) is required');
|
|
244
|
+
if (errors.length === 0)
|
|
245
|
+
throw new Error('must be at least one error');
|
|
246
|
+
super(`first of ${errors.length} error${errors.length === 1 ? '' : 's'}`, {
|
|
247
|
+
cause: errors[0]
|
|
248
|
+
});
|
|
249
|
+
this.errors = [...errors];
|
|
250
|
+
}
|
|
251
|
+
toJSON() {
|
|
252
|
+
return { ...super.toJSON(), errors: [...this.errors] };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
//# sourceMappingURL=verror.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verror.js","sourceRoot":"","sources":["../src/verror.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,GAAY;IACzC,IAAI,CAAC,CAAC,GAAG,YAAY,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAC9C,2EAA2E;IAC3E,yEAAyE;IACzE,IAAI,OAAQ,GAAc,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QACjD,OAAQ,GAAc,CAAC,MAAM,EAAE,CAAC;IAClC,CAAC;IACD,+EAA+E;IAC/E,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,YAAY,EAAE,GAAG,CAAC,OAAO;QACzB,KAAK,EAAE,cAAc,CAAE,GAA2B,CAAC,KAAK,CAAC;QACzD,IAAI,EAAE,EAAE;KACT,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,MAAO,SAAQ,KAAK;IAC/B,qFAAqF;IACnE,IAAI,GAAW,QAAQ,CAAC;IAE1C;;;;;;;;OAQG;IACK,MAAM,CAAC,sBAAsB,CAAC,KAAY;QAChD,IAAI,KAAK,YAAY,MAAM;YAAE,OAAO,KAAK,CAAC,OAAO,CAAC;QAClD,MAAM,MAAM,GAAI,KAA6B,CAAC,KAAK,CAAC;QACpD,I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-1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polygonlabs/verror",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "TypeScript-first, browser-friendly VError-inspired error handling library",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/0xPolygon/apps-team-packages.git",
|
|
9
|
+
"directory": "packages/verror"
|
|
10
|
+
},
|
|
6
11
|
"publishConfig": {
|
|
7
12
|
"access": "public"
|
|
8
13
|
},
|
|
@@ -12,7 +17,9 @@
|
|
|
12
17
|
"import": "./dist/index.js"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
|
-
"files": [
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
16
23
|
"dependencies": {},
|
|
17
24
|
"devDependencies": {
|
|
18
25
|
"@polygonlabs/apps-team-lint": "^1.0.0",
|