@vltpkg/error-cause 0.0.0-7 → 0.0.0-9
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/esm/index.d.ts +62 -49
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +50 -16
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/dist/esm/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { IncomingHttpHeaders, IncomingMessage } from 'http';
|
|
|
7
7
|
* Several of these types are just very basic duck-typing, because referencing
|
|
8
8
|
* internal types directly would create a workspace dependency cycle.
|
|
9
9
|
*/
|
|
10
|
-
export type
|
|
10
|
+
export type ErrorCauseOptions = {
|
|
11
11
|
/**
|
|
12
12
|
* The `cause` field within a `cause` object should
|
|
13
13
|
* always be an `Error` object that was previously thrown. Note
|
|
@@ -17,7 +17,7 @@ export type ErrorCauseObject = {
|
|
|
17
17
|
* because we use `useUnknownInCatchVariables` so this makes it easier
|
|
18
18
|
* to rethrow a caught error without recasting it.
|
|
19
19
|
*/
|
|
20
|
-
cause?:
|
|
20
|
+
cause?: unknown;
|
|
21
21
|
/** the name of something */
|
|
22
22
|
name?: string;
|
|
23
23
|
/** byte offset in a Buffer or file */
|
|
@@ -63,7 +63,7 @@ export type ErrorCauseObject = {
|
|
|
63
63
|
* Array of valid options when something is not a valid option.
|
|
64
64
|
* (For use in `did you mean X?` output.)
|
|
65
65
|
*/
|
|
66
|
-
validOptions?:
|
|
66
|
+
validOptions?: unknown[];
|
|
67
67
|
/**
|
|
68
68
|
* message indicating what bit of work this might be a part of, what feature
|
|
69
69
|
* needs to be implemented, etc. Eg, `{ todo: 'nested workspace support' }`.
|
|
@@ -73,9 +73,9 @@ export type ErrorCauseObject = {
|
|
|
73
73
|
* A desired value that was not found, or a regular expression or other
|
|
74
74
|
* pattern describing it.
|
|
75
75
|
*/
|
|
76
|
-
wanted?:
|
|
76
|
+
wanted?: unknown;
|
|
77
77
|
/** actual value, which was not wanted */
|
|
78
|
-
found?:
|
|
78
|
+
found?: unknown;
|
|
79
79
|
/** HTTP message, fetch.Response, or `@vltpkg/registry-client.CacheEntry` */
|
|
80
80
|
response?: IncomingMessage | Response | {
|
|
81
81
|
statusCode: number;
|
|
@@ -112,9 +112,9 @@ export type ErrorCauseObject = {
|
|
|
112
112
|
time?: Record<string, string>;
|
|
113
113
|
};
|
|
114
114
|
/** maximum value, which was exceeded */
|
|
115
|
-
max?:
|
|
115
|
+
max?: unknown;
|
|
116
116
|
/** minimum value, which was not met */
|
|
117
|
-
min?:
|
|
117
|
+
min?: unknown;
|
|
118
118
|
};
|
|
119
119
|
export type DuckTypeManifest = Record<string, any> & {
|
|
120
120
|
name?: string;
|
|
@@ -135,62 +135,75 @@ export type DuckTypeManifest = Record<string, any> & {
|
|
|
135
135
|
}[];
|
|
136
136
|
};
|
|
137
137
|
};
|
|
138
|
-
export type ErrorCause = Error | ErrorCauseObject;
|
|
139
138
|
/**
|
|
140
|
-
*
|
|
141
|
-
*
|
|
139
|
+
* The input cause for the {@link error} functions. Can either be a plain error
|
|
140
|
+
* or an error cause options object.
|
|
142
141
|
*/
|
|
143
|
-
export type
|
|
144
|
-
|
|
142
|
+
export type ErrorCause = Error | ErrorCauseOptions;
|
|
143
|
+
/**
|
|
144
|
+
* The same as {@link ErrorCauseOptions} except where `cause` has been
|
|
145
|
+
* converted to an Error.
|
|
146
|
+
*/
|
|
147
|
+
export type ErrorCauseResult = Omit<ErrorCauseOptions, 'cause'> & {
|
|
148
|
+
cause?: Error;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* An error with a cause property. Cause defaults to `unknown`.
|
|
152
|
+
*/
|
|
153
|
+
export type ErrorWithCause<T extends Error = Error, U = unknown> = T & {
|
|
154
|
+
cause: U;
|
|
145
155
|
};
|
|
146
156
|
/**
|
|
147
|
-
*
|
|
148
|
-
* another nested error
|
|
157
|
+
* An error with a cause property that is an Error.
|
|
149
158
|
*/
|
|
159
|
+
export type ErrorWithCauseError<T extends Error = Error> = ErrorWithCause<T, Error>;
|
|
150
160
|
/**
|
|
151
|
-
*
|
|
152
|
-
* because all properties of the cause are optional.
|
|
161
|
+
* An error with a cause property that is an {@link ErrorCauseResult}.
|
|
153
162
|
*/
|
|
154
|
-
export
|
|
163
|
+
export type ErrorWithCauseObject<T extends Error = Error> = ErrorWithCause<T, ErrorCauseResult>;
|
|
155
164
|
/**
|
|
156
|
-
*
|
|
165
|
+
* Helper util to convert unknown to a plain error. Not specifically
|
|
166
|
+
* related to error causes, but useful for error handling in general.
|
|
157
167
|
*/
|
|
158
|
-
export declare const
|
|
159
|
-
|
|
168
|
+
export declare const asError: (er: unknown, fallbackMessage?: string) => Error;
|
|
169
|
+
/**
|
|
170
|
+
* Helper util to check if an error has any type of cause property.
|
|
171
|
+
* Note that this does not mean it is a cause from this library,
|
|
172
|
+
* just that it has a cause property.
|
|
173
|
+
*/
|
|
174
|
+
export declare const isErrorWithCause: (er: unknown) => er is ErrorWithCause;
|
|
175
|
+
export declare const errorCodes: readonly ["EEXIST", "EINTEGRITY", "EINVAL", "ELIFECYCLE", "EMAXREDIRECT", "ENEEDAUTH", "ENOENT", "ENOGIT", "ERESOLVE", "EUNKNOWN", "EUSAGE"];
|
|
160
176
|
/**
|
|
161
177
|
* Valid properties for the 'code' field in an Error cause.
|
|
162
178
|
* Add new options to this list as needed.
|
|
163
179
|
*/
|
|
164
|
-
export type Codes =
|
|
165
|
-
|
|
180
|
+
export type Codes = (typeof errorCodes)[number];
|
|
181
|
+
/**
|
|
182
|
+
* An error with a cause property that is an {@link ErrorCauseResult}
|
|
183
|
+
* and has a code property that is a {@link Codes}.
|
|
184
|
+
*/
|
|
185
|
+
export type ErrorWithCode<T extends Error = Error> = ErrorWithCause<T, Omit<ErrorCauseResult, 'code'> & {
|
|
186
|
+
code: Exclude<ErrorCauseResult['code'], undefined>;
|
|
187
|
+
}>;
|
|
188
|
+
/**
|
|
189
|
+
* Type guard to check if an error has one of our error code properties.
|
|
190
|
+
* Note that the type check only checks for the code property and value,
|
|
191
|
+
* but since every property on {@link ErrorCauseOptions} is optional, this should
|
|
192
|
+
* be sufficient to know the shape of the cause and use it in printing.
|
|
193
|
+
*/
|
|
194
|
+
export declare const isErrorWithCode: (er: unknown) => er is ErrorWithCode;
|
|
195
|
+
export type From = Function;
|
|
196
|
+
export type ErrorCtor<T extends Error> = new (message: string, options?: {
|
|
197
|
+
cause: Error | ErrorCauseResult;
|
|
198
|
+
}) => T;
|
|
199
|
+
export type ErrorResult<T extends Error = Error> = T | ErrorWithCauseError<TypeError> | ErrorWithCauseObject<TypeError>;
|
|
166
200
|
export declare function error(message: string, cause?: undefined, from?: From): Error;
|
|
167
|
-
export declare function error(message: string, cause:
|
|
168
|
-
|
|
169
|
-
};
|
|
170
|
-
export declare function error(message: string, cause: Error, from?: From): Error & {
|
|
171
|
-
cause: Error;
|
|
172
|
-
};
|
|
173
|
-
export declare function error(message: string, cause: ErrorCause, from?: From): Error & {
|
|
174
|
-
cause: ErrorCause;
|
|
175
|
-
};
|
|
201
|
+
export declare function error(message: string, cause: Error, from?: From): ErrorWithCauseError;
|
|
202
|
+
export declare function error(message: string, cause: ErrorCauseOptions, from?: From): ErrorWithCauseObject;
|
|
176
203
|
export declare function typeError(message: string, cause?: undefined, from?: From): TypeError;
|
|
177
|
-
export declare function typeError(message: string, cause:
|
|
178
|
-
|
|
179
|
-
};
|
|
180
|
-
export declare function typeError(message: string, cause: Error, from?: From): TypeError & {
|
|
181
|
-
cause: Error;
|
|
182
|
-
};
|
|
183
|
-
export declare function typeError(message: string, cause: ErrorCause, from?: From): TypeError & {
|
|
184
|
-
cause: ErrorCause;
|
|
185
|
-
};
|
|
204
|
+
export declare function typeError(message: string, cause: Error, from?: From): ErrorWithCauseError<TypeError>;
|
|
205
|
+
export declare function typeError(message: string, cause: ErrorCauseOptions, from?: From): ErrorWithCauseObject<TypeError>;
|
|
186
206
|
export declare function syntaxError(message: string, cause?: undefined, from?: From): SyntaxError;
|
|
187
|
-
export declare function syntaxError(message: string, cause:
|
|
188
|
-
|
|
189
|
-
};
|
|
190
|
-
export declare function syntaxError(message: string, cause: Error, from?: From): SyntaxError & {
|
|
191
|
-
cause: Error;
|
|
192
|
-
};
|
|
193
|
-
export declare function syntaxError(message: string, cause: ErrorCause, from?: From): SyntaxError & {
|
|
194
|
-
cause: ErrorCause;
|
|
195
|
-
};
|
|
207
|
+
export declare function syntaxError(message: string, cause: Error, from?: From): ErrorWithCauseError<SyntaxError>;
|
|
208
|
+
export declare function syntaxError(message: string, cause: ErrorCauseOptions, from?: From): ErrorWithCauseObject<SyntaxError>;
|
|
196
209
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AAEhE;;;;;;;GAOG;AACH,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AAEhE;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;;;;;;OAQG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IAEf,4BAA4B;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,sCAAsC;IACtC,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;;OAIG;IACH,IAAI,CAAC,EAAE,KAAK,CAAA;IAEZ,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf,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BAAgB,SAAS,CACvB,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,SAAS,EACjB,IAAI,CAAC,EAAE,IAAI,GACV,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,KAAK,EACZ,IAAI,CAAC,EAAE,IAAI,GACV,mBAAmB,CAAC,SAAS,CAAC,CAAA;AACjC,wBAAgB,SAAS,CACvB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,iBAAiB,EACxB,IAAI,CAAC,EAAE,IAAI,GACV,oBAAoB,CAAC,SAAS,CAAC,CAAA;AASlC,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,SAAS,EACjB,IAAI,CAAC,EAAE,IAAI,GACV,WAAW,CAAA;AACd,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,KAAK,EACZ,IAAI,CAAC,EAAE,IAAI,GACV,mBAAmB,CAAC,WAAW,CAAC,CAAA;AACnC,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,iBAAiB,EACxB,IAAI,CAAC,EAAE,IAAI,GACV,oBAAoB,CAAC,WAAW,CAAC,CAAA"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,26 +1,60 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Helper util to convert unknown to a plain error. Not specifically
|
|
3
|
+
* related to error causes, but useful for error handling in general.
|
|
4
4
|
*/
|
|
5
|
+
export const asError = (er, fallbackMessage = 'Unknown error') => er instanceof Error ? er : new Error(String(er) || fallbackMessage);
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
7
|
+
* Helper util to check if an error has any type of cause property.
|
|
8
|
+
* Note that this does not mean it is a cause from this library,
|
|
9
|
+
* just that it has a cause property.
|
|
8
10
|
*/
|
|
9
|
-
export const
|
|
11
|
+
export const isErrorWithCause = (er) => er instanceof Error && 'cause' in er;
|
|
12
|
+
export const errorCodes = [
|
|
13
|
+
'EEXIST',
|
|
14
|
+
'EINTEGRITY',
|
|
15
|
+
'EINVAL',
|
|
16
|
+
'ELIFECYCLE',
|
|
17
|
+
'EMAXREDIRECT',
|
|
18
|
+
'ENEEDAUTH',
|
|
19
|
+
'ENOENT',
|
|
20
|
+
'ENOGIT',
|
|
21
|
+
'ERESOLVE',
|
|
22
|
+
'EUNKNOWN',
|
|
23
|
+
'EUSAGE',
|
|
24
|
+
];
|
|
25
|
+
const errorCodesSet = new Set(errorCodes);
|
|
10
26
|
/**
|
|
11
|
-
* Type guard
|
|
27
|
+
* Type guard to check if an error has one of our error code properties.
|
|
28
|
+
* Note that the type check only checks for the code property and value,
|
|
29
|
+
* but since every property on {@link ErrorCauseOptions} is optional, this should
|
|
30
|
+
* be sufficient to know the shape of the cause and use it in printing.
|
|
12
31
|
*/
|
|
13
|
-
export const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
export const isErrorWithCode = (er) => isErrorWithCause(er) &&
|
|
33
|
+
!!er.cause &&
|
|
34
|
+
typeof er.cause === 'object' &&
|
|
35
|
+
'code' in er.cause &&
|
|
36
|
+
typeof er.cause.code === 'string' &&
|
|
37
|
+
errorCodesSet.has(er.cause.code);
|
|
38
|
+
// `captureStackTrace` is non-standard so explicitly type it as possibly
|
|
39
|
+
// undefined since it might be in browsers.
|
|
40
|
+
const { captureStackTrace } = Error;
|
|
20
41
|
function create(cls, defaultFrom, message, cause, from = defaultFrom) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
let er = null;
|
|
43
|
+
if (cause instanceof Error) {
|
|
44
|
+
er = new cls(message, { cause });
|
|
45
|
+
}
|
|
46
|
+
else if (cause && typeof cause === 'object') {
|
|
47
|
+
if ('cause' in cause) {
|
|
48
|
+
cause.cause = asError(cause.cause);
|
|
49
|
+
}
|
|
50
|
+
er = new cls(message, {
|
|
51
|
+
cause: cause,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
er = new cls(message);
|
|
56
|
+
}
|
|
57
|
+
captureStackTrace?.(er, from);
|
|
24
58
|
return er;
|
|
25
59
|
}
|
|
26
60
|
export function error(message, cause, from) {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AA4LA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,CAAU,EACa,EAAE,CACzB,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,EAAW,EACiB,EAAE,CAC9B,EAAE,YAAY,KAAK,IAAI,kBAAkB,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;AAErD,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,EAAW,EAAc,EAAE,CACtD,EAAE,YAAY,KAAK,CAAC,CAAC,CAAC,EAAE;IACxB,CAAC,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE;QAC3B,sDAAsD;QACxD,CAAC,CAAC,IAAI,KAAK;QACP,gEAAgE;QAChE,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,eAAe,CAC7D,CAAA;AAoDL,SAAS,MAAM,CACb,GAAiB,EACjB,WAAgE,EAChE,OAAe,EACf,KAAkB,EAClB,OAAa,WAAW;IAExB,MAAM,EAAE,GAAG,IAAI,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAC1D,uEAAuE;IACvE,KAAK,CAAC,iBAAiB,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;IACnC,OAAO,EAAE,CAAA;AACX,CAAC;AAwBD,MAAM,UAAU,KAAK,CACnB,OAAe,EACf,KAAkB,EAClB,IAAW;IAEX,OAAO,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;AACnD,CAAC;AAsBD,MAAM,UAAU,SAAS,CACvB,OAAe,EACf,KAAkB,EAClB,IAAW;IAEX,OAAO,MAAM,CAAY,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;AACtE,CAAC;AAsBD,MAAM,UAAU,WAAW,CACzB,OAAe,EACf,KAAkB,EAClB,IAAW;IAEX,OAAO,MAAM,CACX,WAAW,EACX,WAAW,EACX,OAAO,EACP,KAAK,EACL,IAAI,CACL,CAAA;AACH,CAAC","sourcesContent":["import type { IncomingHttpHeaders, IncomingMessage } from 'http'\n\n/**\n * Codification of vlt's Error.cause conventions\n *\n * Add new properties to this list as needed.\n *\n * Several of these types are just very basic duck-typing, because referencing\n * internal types directly would create a workspace dependency cycle.\n */\nexport type ErrorCauseObject = {\n /**\n * The `cause` field within a `cause` object should\n * always be an `Error` object that was previously thrown. Note\n * that the `cause` on an Error itself might _also_ be a\n * previously thrown error, if no additional information could be\n * usefully added beyond improving the message. It is typed as `unknown`\n * because we use `useUnknownInCatchVariables` so this makes it easier\n * to rethrow a caught error without recasting it.\n */\n cause?: ErrorCause | unknown // eslint-disable-line @typescript-eslint/no-redundant-type-constituents\n\n /** the name of something */\n name?: string\n\n /** byte offset in a Buffer or file */\n offset?: number\n\n /**\n * This should only be a string code that we set. See {@link Codes} for\n * the supported options. Lower-level system codes like `ENOENT` should\n * remain on the errors that generated them.\n */\n code?: Codes\n\n /** target of a file system operation */\n path?: string\n\n /**\n * file path origin of a resolution that failed, for example in the case\n * of `file://` specifiers.\n */\n from?: string\n\n /** path on disk that is being written, linked, or extracted to */\n target?: string\n\n /** Spec object/string relevant to an operation that failed */\n spec?:\n | string\n | {\n type: 'file' | 'git' | 'registry' | 'remote' | 'workspace'\n spec: string\n [k: number | string | symbol]: any\n }\n\n /** exit code of a process, or HTTP response status code */\n status?: number | null\n\n /** null or a signal that a process received */\n signal?: NodeJS.Signals | null\n\n /** the root of a project */\n projectRoot?: string\n\n /** the current working directory of a process */\n cwd?: string\n\n /** a command being run in a child process */\n command?: string\n\n /** the arguments passed to a process */\n args?: string[]\n\n /** standard output from a process */\n stdout?: Buffer | string | null\n\n /** standard error from a process */\n stderr?: Buffer | string | null\n\n /**\n * Array of valid options when something is not a valid option.\n * (For use in `did you mean X?` output.)\n */\n validOptions?: any[]\n\n /**\n * message indicating what bit of work this might be a part of, what feature\n * needs to be implemented, etc. Eg, `{ todo: 'nested workspace support' }`.\n */\n todo?: string\n\n /**\n * A desired value that was not found, or a regular expression or other\n * pattern describing it.\n */\n wanted?: any\n\n /** actual value, which was not wanted */\n found?: any\n\n /** HTTP message, fetch.Response, or `@vltpkg/registry-client.CacheEntry` */\n response?:\n | IncomingMessage\n | Response\n | {\n statusCode: number\n headers:\n | Buffer[]\n | Record<string, string[] | string>\n | IncomingHttpHeaders\n text?: () => string\n [k: number | string | symbol]: any\n }\n\n /** string or URL object */\n url?: URL | string\n\n /** git repository remote or path */\n repository?: string\n\n /** string or `@vltpkg/semver.Version` object */\n version?:\n | string\n | {\n raw: string\n major: number\n minor: number\n patch: number\n [k: number | string | symbol]: any\n }\n\n /** string or `@vltpkg/semver.Range` object */\n range?:\n | string\n | {\n raw: string\n isAny: boolean\n includePrerelease: boolean\n [k: number | string | symbol]: any\n }\n\n /** a package manifest, either from `package.json` or a registry */\n manifest?: DuckTypeManifest\n\n /** registry top-level package document */\n packument?: {\n name: string\n 'dist-tags': Record<string, string>\n versions: Record<string, DuckTypeManifest>\n time?: Record<string, string>\n }\n\n /** maximum value, which was exceeded */\n max?: any\n\n /** minimum value, which was not met */\n min?: any\n}\n\nexport type DuckTypeManifest = Record<string, any> & {\n name?: string\n version?: string\n deprecated?: string\n engines?: Record<string, string>\n os?: string[] | string\n arch?: string[] | string\n dist?: {\n integrity?: string\n shasum?: string\n tarball?: string\n fileCount?: number\n unpackedSize?: number\n signatures?: {\n keyid: string\n sig: string\n }[]\n }\n}\n\nexport type ErrorCause = Error | ErrorCauseObject\n\n/**\n * An error with a cause that is a direct error cause object and not another\n * nested error.\n */\nexport type ErrorWithCauseObject = Error & { cause: ErrorCauseObject }\n\n/**\n * A TypeError with a cause that is a direct error cause object and not\n * another nested error\n */\n\n/**\n * If it is any sort of plain-ish object, assume its an error cause\n * because all properties of the cause are optional.\n */\nexport const isErrorCauseObject = (\n v: unknown,\n): v is ErrorCauseObject =>\n !!v && typeof v === 'object' && !Array.isArray(v)\n\n/**\n * Type guard for {@link ErrorWithCauseObject} type\n */\nexport const isErrorRoot = (\n er: unknown,\n): er is ErrorWithCauseObject =>\n er instanceof Error && isErrorCauseObject(er.cause)\n\nexport const asErrorCause = (er: unknown): ErrorCause =>\n er instanceof Error ? er\n : isErrorCauseObject(er) ? er\n // otherwise, make an error of the stringified message\n : new Error(\n // eslint-disable-next-line @typescript-eslint/no-base-to-string\n er == null ? 'Unknown error' : String(er) || 'Unknown error',\n )\n\n/**\n * Valid properties for the 'code' field in an Error cause.\n * Add new options to this list as needed.\n */\nexport type Codes =\n | 'EEXIST'\n | 'EINTEGRITY'\n | 'EINVAL'\n | 'ELIFECYCLE'\n | 'EMAXREDIRECT'\n | 'ENEEDAUTH'\n | 'ENOENT'\n | 'ENOGIT'\n | 'ERESOLVE'\n | 'EUNKNOWN'\n | 'EUSAGE'\n\ntype ErrorCtor<T extends Error> = new (\n message: string,\n options?: { cause: ErrorCause },\n) => T\n\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: ((...a: any[]) => any) | (new (...a: any[]) => any),\n message: string,\n cause?: undefined,\n from?: From,\n): T\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: ((...a: any[]) => any) | (new (...a: any[]) => any),\n message: string,\n cause?: ErrorCauseObject,\n from?: From,\n): T & { cause: ErrorCauseObject }\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: ((...a: any[]) => any) | (new (...a: any[]) => any),\n message: string,\n cause?: Error,\n from?: From,\n): T & { cause: Error }\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: ((...a: any[]) => any) | (new (...a: any[]) => any),\n message: string,\n cause?: ErrorCause,\n from?: From,\n): T & { cause: ErrorCause }\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: ((...a: any[]) => any) | (new (...a: any[]) => any),\n message: string,\n cause?: ErrorCause,\n from: From = defaultFrom,\n) {\n const er = new cls(message, cause ? { cause } : undefined)\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n Error.captureStackTrace?.(er, from)\n return er\n}\n\nexport type From = ((...a: any[]) => any) | (new (...a: any[]) => any)\n\nexport function error(\n message: string,\n cause?: undefined,\n from?: From,\n): Error\nexport function error(\n message: string,\n cause: ErrorCauseObject,\n from?: From,\n): Error & { cause: ErrorCauseObject }\nexport function error(\n message: string,\n cause: Error,\n from?: From,\n): Error & { cause: Error }\nexport function error(\n message: string,\n cause: ErrorCause,\n from?: From,\n): Error & { cause: ErrorCause }\nexport function error(\n message: string,\n cause?: ErrorCause,\n from?: From,\n) {\n return create(Error, error, message, cause, from)\n}\n\nexport function typeError(\n message: string,\n cause?: undefined,\n from?: From,\n): TypeError\nexport function typeError(\n message: string,\n cause: ErrorCauseObject,\n from?: From,\n): TypeError & { cause: ErrorCauseObject }\nexport function typeError(\n message: string,\n cause: Error,\n from?: From,\n): TypeError & { cause: Error }\nexport function typeError(\n message: string,\n cause: ErrorCause,\n from?: From,\n): TypeError & { cause: ErrorCause }\nexport function typeError(\n message: string,\n cause?: ErrorCause,\n from?: From,\n) {\n return create<TypeError>(TypeError, typeError, message, cause, from)\n}\n\nexport function syntaxError(\n message: string,\n cause?: undefined,\n from?: From,\n): SyntaxError\nexport function syntaxError(\n message: string,\n cause: ErrorCauseObject,\n from?: From,\n): SyntaxError & { cause: ErrorCauseObject }\nexport function syntaxError(\n message: string,\n cause: Error,\n from?: From,\n): SyntaxError & { cause: Error }\nexport function syntaxError(\n message: string,\n cause: ErrorCause,\n from?: From,\n): SyntaxError & { cause: ErrorCause }\nexport function syntaxError(\n message: string,\n cause?: ErrorCause,\n from?: From,\n) {\n return create<SyntaxError>(\n SyntaxError,\n syntaxError,\n message,\n cause,\n from,\n )\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAsNA;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,EAAW,EACX,eAAe,GAAG,eAAe,EAC1B,EAAE,CACT,EAAE,YAAY,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,eAAe,CAAC,CAAA;AAErE;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,EAAW,EAAwB,EAAE,CACpE,EAAE,YAAY,KAAK,IAAI,OAAO,IAAI,EAAE,CAAA;AAEtC,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,YAAY;IACZ,cAAc;IACd,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,UAAU;IACV,UAAU;IACV,QAAQ;CACA,CAAA;AAEV,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAA;AAmBzC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,EAAW,EAAuB,EAAE,CAClE,gBAAgB,CAAC,EAAE,CAAC;IACpB,CAAC,CAAC,EAAE,CAAC,KAA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;IAEX,OAAO,MAAM,CAAY,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;AACtE,CAAC;AAiBD,MAAM,UAAU,WAAW,CACzB,OAAe,EACf,KAAkB,EAClB,IAAW;IAEX,OAAO,MAAM,CACX,WAAW,EACX,WAAW,EACX,OAAO,EACP,KAAK,EACL,IAAI,CACL,CAAA;AACH,CAAC","sourcesContent":["import type { IncomingHttpHeaders, IncomingMessage } from 'http'\n\n/**\n * Codification of vlt's Error.cause conventions\n *\n * Add new properties to this list as needed.\n *\n * Several of these types are just very basic duck-typing, because referencing\n * internal types directly would create a workspace dependency cycle.\n */\nexport type ErrorCauseOptions = {\n /**\n * The `cause` field within a `cause` object should\n * always be an `Error` object that was previously thrown. Note\n * that the `cause` on an Error itself might _also_ be a\n * previously thrown error, if no additional information could be\n * usefully added beyond improving the message. It is typed as `unknown`\n * because we use `useUnknownInCatchVariables` so this makes it easier\n * to rethrow a caught error without recasting it.\n */\n cause?: unknown\n\n /** the name of something */\n name?: string\n\n /** byte offset in a Buffer or file */\n offset?: number\n\n /**\n * This should only be a string code that we set. See {@link Codes} for\n * the supported options. Lower-level system codes like `ENOENT` should\n * remain on the errors that generated them.\n */\n code?: Codes\n\n /** target of a file system operation */\n path?: string\n\n /**\n * file path origin of a resolution that failed, for example in the case\n * of `file://` specifiers.\n */\n from?: string\n\n /** path on disk that is being written, linked, or extracted to */\n target?: string\n\n /** Spec object/string relevant to an operation that failed */\n spec?:\n | string\n | {\n type: 'file' | 'git' | 'registry' | 'remote' | 'workspace'\n spec: string\n [k: number | string | symbol]: any\n }\n\n /** exit code of a process, or HTTP response status code */\n status?: number | null\n\n /** null or a signal that a process received */\n signal?: NodeJS.Signals | null\n\n /** the root of a project */\n projectRoot?: string\n\n /** the current working directory of a process */\n cwd?: string\n\n /** a command being run in a child process */\n command?: string\n\n /** the arguments passed to a process */\n args?: string[]\n\n /** standard output from a process */\n stdout?: Buffer | string | null\n\n /** standard error from a process */\n stderr?: Buffer | string | null\n\n /**\n * Array of valid options when something is not a valid option.\n * (For use in `did you mean X?` output.)\n */\n validOptions?: unknown[]\n\n /**\n * message indicating what bit of work this might be a part of, what feature\n * needs to be implemented, etc. Eg, `{ todo: 'nested workspace support' }`.\n */\n todo?: string\n\n /**\n * A desired value that was not found, or a regular expression or other\n * pattern describing it.\n */\n wanted?: unknown\n\n /** actual value, which was not wanted */\n found?: unknown\n\n /** HTTP message, fetch.Response, or `@vltpkg/registry-client.CacheEntry` */\n response?:\n | IncomingMessage\n | Response\n | {\n statusCode: number\n headers:\n | Buffer[]\n | Record<string, string[] | string>\n | IncomingHttpHeaders\n text?: () => string\n [k: number | string | symbol]: any\n }\n\n /** string or URL object */\n url?: URL | string\n\n /** git repository remote or path */\n repository?: string\n\n /** string or `@vltpkg/semver.Version` object */\n version?:\n | string\n | {\n raw: string\n major: number\n minor: number\n patch: number\n [k: number | string | symbol]: any\n }\n\n /** string or `@vltpkg/semver.Range` object */\n range?:\n | string\n | {\n raw: string\n isAny: boolean\n includePrerelease: boolean\n [k: number | string | symbol]: any\n }\n\n /** a package manifest, either from `package.json` or a registry */\n manifest?: DuckTypeManifest\n\n /** registry top-level package document */\n packument?: {\n name: string\n 'dist-tags': Record<string, string>\n versions: Record<string, DuckTypeManifest>\n time?: Record<string, string>\n }\n\n /** maximum value, which was exceeded */\n max?: unknown\n\n /** minimum value, which was not met */\n min?: unknown\n}\n\nexport type DuckTypeManifest = Record<string, any> & {\n name?: string\n version?: string\n deprecated?: string\n engines?: Record<string, string>\n os?: string[] | string\n arch?: string[] | string\n dist?: {\n integrity?: string\n shasum?: string\n tarball?: string\n fileCount?: number\n unpackedSize?: number\n signatures?: {\n keyid: string\n sig: string\n }[]\n }\n}\n\n/**\n * The input cause for the {@link error} functions. Can either be a plain error\n * or an error cause options object.\n */\nexport type ErrorCause = Error | ErrorCauseOptions\n\n/**\n * The same as {@link ErrorCauseOptions} except where `cause` has been\n * converted to an Error.\n */\nexport type ErrorCauseResult = Omit<ErrorCauseOptions, 'cause'> & {\n cause?: Error\n}\n\n/**\n * An error with a cause property. Cause defaults to `unknown`.\n */\nexport type ErrorWithCause<\n T extends Error = Error,\n U = unknown,\n> = T & { cause: U }\n\n/**\n * An error with a cause property that is an Error.\n */\nexport type ErrorWithCauseError<T extends Error = Error> =\n ErrorWithCause<T, Error>\n\n/**\n * An error with a cause property that is an {@link ErrorCauseResult}.\n */\nexport type ErrorWithCauseObject<T extends Error = Error> =\n ErrorWithCause<T, ErrorCauseResult>\n\n/**\n * Helper util to convert unknown to a plain error. Not specifically\n * related to error causes, but useful for error handling in general.\n */\nexport const asError = (\n er: unknown,\n fallbackMessage = 'Unknown error',\n): Error =>\n er instanceof Error ? er : new Error(String(er) || fallbackMessage)\n\n/**\n * Helper util to check if an error has any type of cause property.\n * Note that this does not mean it is a cause from this library,\n * just that it has a cause property.\n */\nexport const isErrorWithCause = (er: unknown): er is ErrorWithCause =>\n er instanceof Error && 'cause' in er\n\nexport const errorCodes = [\n 'EEXIST',\n 'EINTEGRITY',\n 'EINVAL',\n 'ELIFECYCLE',\n 'EMAXREDIRECT',\n 'ENEEDAUTH',\n 'ENOENT',\n 'ENOGIT',\n 'ERESOLVE',\n 'EUNKNOWN',\n 'EUSAGE',\n] as const\n\nconst errorCodesSet = new Set(errorCodes)\n\n/**\n * Valid properties for the 'code' field in an Error cause.\n * Add new options to this list as needed.\n */\nexport type Codes = (typeof errorCodes)[number]\n\n/**\n * An error with a cause property that is an {@link ErrorCauseResult}\n * and has a code property that is a {@link Codes}.\n */\nexport type ErrorWithCode<T extends Error = Error> = ErrorWithCause<\n T,\n Omit<ErrorCauseResult, 'code'> & {\n code: Exclude<ErrorCauseResult['code'], undefined>\n }\n>\n\n/**\n * Type guard to check if an error has one of our error code properties.\n * Note that the type check only checks for the code property and value,\n * but since every property on {@link ErrorCauseOptions} is optional, this should\n * be sufficient to know the shape of the cause and use it in printing.\n */\nexport const isErrorWithCode = (er: unknown): er is ErrorWithCode =>\n isErrorWithCause(er) &&\n !!er.cause &&\n typeof er.cause === 'object' &&\n 'code' in er.cause &&\n typeof er.cause.code === 'string' &&\n errorCodesSet.has(er.cause.code as Codes)\n\n// Use `Function` because that is the same type as `Error.captureStackTrace`\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport type From = Function\n\n// `captureStackTrace` is non-standard so explicitly type it as possibly\n// undefined since it might be in browsers.\nconst { captureStackTrace } = Error as {\n captureStackTrace?: ErrorConstructor['captureStackTrace']\n}\n\nexport type ErrorCtor<T extends Error> = new (\n message: string,\n options?: { cause: Error | ErrorCauseResult },\n) => T\n\nexport type ErrorResult<T extends Error = Error> =\n | T\n | ErrorWithCauseError<TypeError>\n | ErrorWithCauseObject<TypeError>\n\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: From,\n message: string,\n cause?: undefined,\n from?: From,\n): T\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: From,\n message: string,\n cause?: Error,\n from?: From,\n): ErrorWithCauseError<T>\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: From,\n message: string,\n cause?: ErrorCauseOptions,\n from?: From,\n): ErrorWithCauseObject<T>\nfunction create<T extends Error>(\n cls: ErrorCtor<T>,\n defaultFrom: From,\n message: string,\n cause?: ErrorCause,\n from: From = defaultFrom,\n) {\n let er: T | null = null\n if (cause instanceof Error) {\n er = new cls(message, { cause })\n } else if (cause && typeof cause === 'object') {\n if ('cause' in cause) {\n cause.cause = asError(cause.cause)\n }\n er = new cls(message, {\n cause: cause as ErrorCauseResult,\n })\n } else {\n er = new cls(message)\n }\n captureStackTrace?.(er, from)\n return er\n}\n\nexport function error(\n message: string,\n cause?: undefined,\n from?: From,\n): Error\nexport function error(\n message: string,\n cause: Error,\n from?: From,\n): ErrorWithCauseError\nexport function error(\n message: string,\n cause: ErrorCauseOptions,\n from?: From,\n): ErrorWithCauseObject\nexport function error(\n message: string,\n cause?: ErrorCause,\n from?: From,\n): ErrorResult {\n return create(Error, error, message, cause, from)\n}\n\nexport function typeError(\n message: string,\n cause?: undefined,\n from?: From,\n): TypeError\nexport function typeError(\n message: string,\n cause: Error,\n from?: From,\n): ErrorWithCauseError<TypeError>\nexport function typeError(\n message: string,\n cause: ErrorCauseOptions,\n from?: From,\n): ErrorWithCauseObject<TypeError>\nexport function typeError(\n message: string,\n cause?: ErrorCause,\n from?: From,\n): ErrorResult<TypeError> {\n return create<TypeError>(TypeError, typeError, message, cause, from)\n}\n\nexport function syntaxError(\n message: string,\n cause?: undefined,\n from?: From,\n): SyntaxError\nexport function syntaxError(\n message: string,\n cause: Error,\n from?: From,\n): ErrorWithCauseError<SyntaxError>\nexport function syntaxError(\n message: string,\n cause: ErrorCauseOptions,\n from?: From,\n): ErrorWithCauseObject<SyntaxError>\nexport function syntaxError(\n message: string,\n cause?: ErrorCause,\n from?: From,\n): ErrorResult<SyntaxError> {\n return create<SyntaxError>(\n SyntaxError,\n syntaxError,\n message,\n cause,\n from,\n )\n}\n"]}
|