@idlebox/errors 0.1.12 → 0.1.14
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/lib/autoindex.d.ts +1 -0
- package/lib/autoindex.d.ts.map +1 -1
- package/lib/autoindex.js +0 -2
- package/lib/autoindex.js.map +1 -1
- package/lib/codes/wellknown-exit-codes.d.ts +4 -0
- package/lib/codes/wellknown-exit-codes.d.ts.map +1 -1
- package/lib/codes/wellknown-exit-codes.js +4 -0
- package/lib/codes/wellknown-exit-codes.js.map +1 -1
- package/lib/common/application.d.ts +4 -4
- package/lib/common/application.d.ts.map +1 -1
- package/lib/common/application.js +6 -6
- package/lib/common/application.js.map +1 -1
- package/lib/common/base.d.ts +27 -9
- package/lib/common/base.d.ts.map +1 -1
- package/lib/common/base.js +26 -20
- package/lib/common/base.js.map +1 -1
- package/lib/common/development.d.ts +3 -5
- package/lib/common/development.d.ts.map +1 -1
- package/lib/common/development.js +9 -12
- package/lib/common/development.js.map +1 -1
- package/package.json +3 -3
- package/src/autoindex.ts +1 -0
- package/src/codes/wellknown-exit-codes.ts +5 -0
- package/src/common/application.ts +7 -7
- package/src/common/base.ts +46 -23
- package/src/common/development.ts +11 -17
package/lib/autoindex.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { ExitCode } from "./codes/wellknown-exit-codes.js";
|
|
|
13
13
|
export { Exit } from "./common/application.js";
|
|
14
14
|
export { InterruptError } from "./common/application.js";
|
|
15
15
|
export { UsageError } from "./common/application.js";
|
|
16
|
+
export type { IErrorOptions } from "./common/base.js";
|
|
16
17
|
export { humanReadable } from "./common/base.js";
|
|
17
18
|
export type { IHumanReadable } from "./common/base.js";
|
|
18
19
|
export { isHumanReadableError } from "./common/base.js";
|
package/lib/autoindex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"autoindex.d.ts","sourceRoot":"","sources":["../src/autoindex.ts"],"names":[],"mappings":"AAAA;;;;;;;g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EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD,YAAY,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC"}
|
package/lib/autoindex.js
CHANGED
|
@@ -26,8 +26,6 @@ export { ExitCode } from "./codes/wellknown-exit-codes.js";
|
|
|
26
26
|
export { Exit } from "./common/application.js";
|
|
27
27
|
export { InterruptError } from "./common/application.js";
|
|
28
28
|
export { UsageError } from "./common/application.js";
|
|
29
|
-
/* common/base.ts */
|
|
30
|
-
// Identifiers
|
|
31
29
|
export { humanReadable } from "./common/base.js";
|
|
32
30
|
export { isHumanReadableError } from "./common/base.js";
|
|
33
31
|
export { NotError } from "./common/base.js";
|
package/lib/autoindex.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"autoindex.js","sourceRoot":"","sources":["../src/autoindex.ts"],"names":[],"mappings":"AAAA;;;;;;;gFAOgF;AAChF,8BAA8B;AAC9B,wCAAwC;AACxC,0CAA0C;AAC1C,oBAAoB;AACpB,aAAa;AAEb,gCAAgC;AAC/B,cAAc;AACd,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC/D,+BAA+B;AAC9B,cAAc;AACd,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,gBA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AAC;AACjD,2BAA2B;AAC1B,cAAc;AACd,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAIpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAClD,gCAAgC;AAC/B,cAAc;AACd,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wellknown-exit-codes.d.ts","sourceRoot":"","sources":["../../src/codes/wellknown-exit-codes.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ;IACnB;;OAEG;IACH,SAAS,IAAI;IAEb;;OAEG;IACH,SAAS,IAAI;IAEb;;OAEG;IACH,KAAK,IAAI;IAET;;OAEG;IACH,OAAO,KAAK;CACZ"}
|
|
1
|
+
{"version":3,"file":"wellknown-exit-codes.d.ts","sourceRoot":"","sources":["../../src/codes/wellknown-exit-codes.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ;IACnB;;OAEG;IACH,SAAS,IAAI;IAEb;;OAEG;IACH,SAAS,IAAI;IAEb;;OAEG;IACH,KAAK,IAAI;IAET;;OAEG;IACH,OAAO,IAAI;IAEX;;OAEG;IACH,OAAO,KAAK;CACZ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wellknown-exit-codes.js","sourceRoot":"","sources":["../../src/codes/wellknown-exit-codes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,
|
|
1
|
+
{"version":3,"file":"wellknown-exit-codes.js","sourceRoot":"","sources":["../../src/codes/wellknown-exit-codes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,QAyBX;AAzBD,WAAY,QAAQ;IACnB;;OAEG;IACH,iDAAa,CAAA;IAEb;;OAEG;IACH,iDAAa,CAAA;IAEb;;OAEG;IACH,yCAAS,CAAA;IAET;;OAEG;IACH,6CAAW,CAAA;IAEX;;OAEG;IACH,8CAAY,CAAA;AACb,CAAC,EAzBW,QAAQ,KAAR,QAAQ,QAyBnB"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ErrorWithCode, humanReadable } from './base.js';
|
|
1
|
+
import { ErrorWithCode, humanReadable, type IErrorOptions } from './base.js';
|
|
2
2
|
/**
|
|
3
3
|
* 程序因为正常运行结束而退出
|
|
4
4
|
* catch到此错误时应直接重新抛出,不应做其他处理
|
|
5
5
|
*/
|
|
6
6
|
export declare class Exit extends ErrorWithCode {
|
|
7
|
-
constructor(code: number,
|
|
7
|
+
constructor(code: number, opts?: IErrorOptions);
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* SIGINT (Ctrl+C)
|
|
@@ -12,14 +12,14 @@ export declare class Exit extends ErrorWithCode {
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class InterruptError extends ErrorWithCode {
|
|
14
14
|
readonly signal: NodeJS.Signals;
|
|
15
|
-
constructor(signal: NodeJS.Signals,
|
|
15
|
+
constructor(signal: NodeJS.Signals, opts?: IErrorOptions);
|
|
16
16
|
[humanReadable](): string;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* 由于错误的参数、配置导致错误
|
|
20
20
|
*/
|
|
21
21
|
export declare class UsageError extends ErrorWithCode {
|
|
22
|
-
constructor(message: string,
|
|
22
|
+
constructor(message: string, opts?: IErrorOptions);
|
|
23
23
|
[humanReadable](): string;
|
|
24
24
|
}
|
|
25
25
|
//# sourceMappingURL=application.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"application.d.ts","sourceRoot":"","sources":["../../src/common/application.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"application.d.ts","sourceRoot":"","sources":["../../src/common/application.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAE7E;;;GAGG;AACH,qBAAa,IAAK,SAAQ,aAAa;gBAC1B,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa;CAG9C;AAED;;;GAGG;AACH,qBAAa,cAAe,SAAQ,aAAa;aAE/B,MAAM,EAAE,MAAM,CAAC,OAAO;gBAAtB,MAAM,EAAE,MAAM,CAAC,OAAO,EACtC,IAAI,CAAC,EAAE,aAAa;IAKZ,CAAC,aAAa,CAAC;CAGxB;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,aAAa;gBAChC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa;IAIxC,CAAC,aAAa,CAAC;CAGxB"}
|
|
@@ -5,8 +5,8 @@ import { ErrorWithCode, humanReadable } from './base.js';
|
|
|
5
5
|
* catch到此错误时应直接重新抛出,不应做其他处理
|
|
6
6
|
*/
|
|
7
7
|
export class Exit extends ErrorWithCode {
|
|
8
|
-
constructor(code,
|
|
9
|
-
super(`process exit with code ${code}`, code,
|
|
8
|
+
constructor(code, opts) {
|
|
9
|
+
super(`process exit with code ${code}`, code, opts);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
@@ -15,8 +15,8 @@ export class Exit extends ErrorWithCode {
|
|
|
15
15
|
*/
|
|
16
16
|
export class InterruptError extends ErrorWithCode {
|
|
17
17
|
signal;
|
|
18
|
-
constructor(signal,
|
|
19
|
-
super(`interrupt by signal ${signal}`, ExitCode.INTERRUPT,
|
|
18
|
+
constructor(signal, opts) {
|
|
19
|
+
super(`interrupt by signal ${signal}`, ExitCode.INTERRUPT, opts);
|
|
20
20
|
this.signal = signal;
|
|
21
21
|
}
|
|
22
22
|
[humanReadable]() {
|
|
@@ -27,8 +27,8 @@ export class InterruptError extends ErrorWithCode {
|
|
|
27
27
|
* 由于错误的参数、配置导致错误
|
|
28
28
|
*/
|
|
29
29
|
export class UsageError extends ErrorWithCode {
|
|
30
|
-
constructor(message,
|
|
31
|
-
super(message, ExitCode.USAGE,
|
|
30
|
+
constructor(message, opts) {
|
|
31
|
+
super(message, ExitCode.USAGE, opts);
|
|
32
32
|
}
|
|
33
33
|
[humanReadable]() {
|
|
34
34
|
return `参数错误: ${this.message}\n - 此为非程序性错误,使用 --help 查看帮助`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"application.js","sourceRoot":"","sources":["../../src/common/application.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"application.js","sourceRoot":"","sources":["../../src/common/application.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,aAAa,EAAsB,MAAM,WAAW,CAAC;AAE7E;;;GAGG;AACH,MAAM,OAAO,IAAK,SAAQ,aAAa;IACtC,YAAY,IAAY,EAAE,IAAoB;QAC7C,KAAK,CAAC,0BAA0B,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACrD,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,aAAa;IAE/B;IADjB,YACiB,MAAsB,EACtC,IAAoB;QAEpB,KAAK,CAAC,uBAAuB,MAAM,EAAE,EAAE,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAHjD,WAAM,GAAN,MAAM,CAAgB;IAIvC,CAAC;IAEQ,CAAC,aAAa,CAAC;QACvB,OAAO,SAAS,IAAI,CAAC,MAAM,OAAO,CAAC;IACpC,CAAC;CACD;AAED;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,aAAa;IAC5C,YAAY,OAAe,EAAE,IAAoB;QAChD,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;IAEQ,CAAC,aAAa,CAAC;QACvB,OAAO,SAAS,IAAI,CAAC,OAAO,+BAA+B,CAAC;IAC7D,CAAC;CACD"}
|
package/lib/common/base.d.ts
CHANGED
|
@@ -1,29 +1,45 @@
|
|
|
1
|
+
export interface IErrorOptions {
|
|
2
|
+
/**
|
|
3
|
+
* stack属性的边界函数
|
|
4
|
+
* @see {Error.captureStackTrace}
|
|
5
|
+
*/
|
|
6
|
+
boundary?: CallableFunction;
|
|
7
|
+
/**
|
|
8
|
+
* cause属性的值
|
|
9
|
+
* @see {Error.cause}
|
|
10
|
+
*/
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
}
|
|
1
13
|
export declare const humanReadable: unique symbol;
|
|
2
14
|
export interface IHumanReadable {
|
|
3
15
|
[humanReadable](): string;
|
|
4
16
|
}
|
|
5
17
|
export declare function isHumanReadableError(error: unknown): error is IHumanReadable;
|
|
6
|
-
|
|
18
|
+
/**
|
|
19
|
+
* 利用try...catch统一接口,或是用于分支条件等,未发生错误的情况
|
|
20
|
+
*/
|
|
21
|
+
export declare class NotError implements IHumanReadable {
|
|
22
|
+
readonly extra_message: string;
|
|
7
23
|
constructor(extra_message?: string);
|
|
8
|
-
get stack():
|
|
24
|
+
get stack(): void;
|
|
25
|
+
get message(): void;
|
|
9
26
|
[humanReadable](): string;
|
|
10
27
|
}
|
|
11
28
|
/**
|
|
12
|
-
* 应用程序退出
|
|
13
|
-
* 此错误通常不需要输出到日志
|
|
14
29
|
* @abstract
|
|
15
30
|
*/
|
|
16
31
|
export declare class ErrorWithCode extends Error implements IHumanReadable {
|
|
17
32
|
readonly code: number;
|
|
18
|
-
constructor(message: string, code: number,
|
|
33
|
+
constructor(message: string, code: number, opts?: IErrorOptions);
|
|
34
|
+
get name(): string;
|
|
19
35
|
[humanReadable](): string;
|
|
20
36
|
}
|
|
21
37
|
/**
|
|
22
38
|
* Error when something.cancel() is called
|
|
23
39
|
* @public
|
|
24
40
|
*/
|
|
25
|
-
export declare class CanceledError extends
|
|
26
|
-
constructor();
|
|
41
|
+
export declare class CanceledError extends ErrorWithCode implements IHumanReadable {
|
|
42
|
+
constructor(opts?: IErrorOptions);
|
|
27
43
|
static is(e: unknown): e is CanceledError;
|
|
28
44
|
[humanReadable](): string;
|
|
29
45
|
}
|
|
@@ -31,11 +47,13 @@ export declare class CanceledError extends Error implements IHumanReadable {
|
|
|
31
47
|
* Error when something timeout
|
|
32
48
|
* @public
|
|
33
49
|
*/
|
|
34
|
-
export declare class TimeoutError extends
|
|
50
|
+
export declare class TimeoutError extends ErrorWithCode implements IHumanReadable {
|
|
35
51
|
private readonly ms;
|
|
36
52
|
private readonly why;
|
|
37
53
|
private readonly what?;
|
|
38
|
-
constructor(ms: number, why?: string,
|
|
54
|
+
constructor(ms: number, why?: string, opts?: IErrorOptions & {
|
|
55
|
+
what?: string;
|
|
56
|
+
});
|
|
39
57
|
static is(error: unknown): error is TimeoutError;
|
|
40
58
|
[humanReadable](): string;
|
|
41
59
|
}
|
package/lib/common/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/common/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/common/base.ts"],"names":[],"mappings":"AAMA,MAAM,WA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E,OAAO,GAAG,KAAK,IAAI,YAAY;IAIvC,CAAC,aAAa,CAAC;CAGxB"}
|
package/lib/common/base.js
CHANGED
|
@@ -1,35 +1,40 @@
|
|
|
1
|
+
import { ExitCode } from '../codes/wellknown-exit-codes.js';
|
|
1
2
|
function noop() { }
|
|
2
3
|
const replaceStackTrace = Error.captureStackTrace ?? noop;
|
|
3
4
|
export const humanReadable = Symbol('humanReadable');
|
|
4
5
|
export function isHumanReadableError(error) {
|
|
5
6
|
return error instanceof Object && humanReadable in error;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* 利用try...catch统一接口,或是用于分支条件等,未发生错误的情况
|
|
10
|
+
*/
|
|
11
|
+
export class NotError {
|
|
12
|
+
extra_message;
|
|
8
13
|
constructor(extra_message = '') {
|
|
9
|
-
|
|
14
|
+
this.extra_message = extra_message;
|
|
10
15
|
}
|
|
11
16
|
get stack() {
|
|
12
|
-
|
|
17
|
+
throw new Error(`NotError 未被正确捕获 [hint: ${this.extra_message}]`);
|
|
18
|
+
}
|
|
19
|
+
get message() {
|
|
20
|
+
throw new Error(`NotError 未被正确捕获 [hint: ${this.extra_message}]`);
|
|
13
21
|
}
|
|
14
22
|
[humanReadable]() {
|
|
15
|
-
return
|
|
23
|
+
return `你不应该看到此消息。你无需尝试处理该错误,请联系开发者。[hint: ${this.extra_message}]`;
|
|
16
24
|
}
|
|
17
25
|
}
|
|
18
26
|
/**
|
|
19
|
-
* 应用程序退出
|
|
20
|
-
* 此错误通常不需要输出到日志
|
|
21
27
|
* @abstract
|
|
22
28
|
*/
|
|
23
29
|
export class ErrorWithCode extends Error {
|
|
24
30
|
code;
|
|
25
|
-
constructor(message, code,
|
|
26
|
-
|
|
27
|
-
Error.stackTraceLimit = 0;
|
|
28
|
-
super(message);
|
|
31
|
+
constructor(message, code, opts) {
|
|
32
|
+
super(message, opts);
|
|
29
33
|
this.code = code;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
replaceStackTrace(this, opts?.boundary ?? this.constructor);
|
|
35
|
+
}
|
|
36
|
+
get name() {
|
|
37
|
+
return this.constructor.name;
|
|
33
38
|
}
|
|
34
39
|
[humanReadable]() {
|
|
35
40
|
let msg;
|
|
@@ -42,13 +47,14 @@ export class ErrorWithCode extends Error {
|
|
|
42
47
|
return `${msg}\n - 但是你不应该看到此信息,开发者忘记处理此种情况`;
|
|
43
48
|
}
|
|
44
49
|
}
|
|
50
|
+
Object.setPrototypeOf(ErrorWithCode.prototype, Error.prototype);
|
|
45
51
|
/**
|
|
46
52
|
* Error when something.cancel() is called
|
|
47
53
|
* @public
|
|
48
54
|
*/
|
|
49
|
-
export class CanceledError extends
|
|
50
|
-
constructor() {
|
|
51
|
-
super('Canceled');
|
|
55
|
+
export class CanceledError extends ErrorWithCode {
|
|
56
|
+
constructor(opts) {
|
|
57
|
+
super('Canceled', ExitCode.INTERRUPT, opts);
|
|
52
58
|
}
|
|
53
59
|
static is(e) {
|
|
54
60
|
return e instanceof CanceledError;
|
|
@@ -61,15 +67,15 @@ export class CanceledError extends Error {
|
|
|
61
67
|
* Error when something timeout
|
|
62
68
|
* @public
|
|
63
69
|
*/
|
|
64
|
-
export class TimeoutError extends
|
|
70
|
+
export class TimeoutError extends ErrorWithCode {
|
|
65
71
|
ms;
|
|
66
72
|
why;
|
|
67
73
|
what;
|
|
68
|
-
constructor(ms, why = 'no response',
|
|
69
|
-
super(`Timeout: ${why} in ${ms}ms${what ? ` (when ${what})` : ''}
|
|
74
|
+
constructor(ms, why = 'no response', opts) {
|
|
75
|
+
super(`Timeout: ${why} in ${ms}ms${opts?.what ? ` (when ${opts.what})` : ''}`, ExitCode.TIMEOUT, opts);
|
|
70
76
|
this.ms = ms;
|
|
71
77
|
this.why = why;
|
|
72
|
-
this.what = what;
|
|
78
|
+
this.what = opts?.what;
|
|
73
79
|
}
|
|
74
80
|
static is(error) {
|
|
75
81
|
return error instanceof TimeoutError;
|
package/lib/common/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/common/base.ts"],"names":[],"mappings":"AAAA,SAAS,IAAI,KAAI,CAAC;AAElB,MAAM,iBAAiB,GAAG,KAAK,CAAC,iBAAiB,IAAI,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/common/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,SAAS,IAAI,KAAI,CAAC;AAElB,MAAM,iBAAiB,GAAG,KAAK,CAAC,iBAAiB,IAAI,IAAI,CAAC;AAgB1D,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAKrD,MAAM,UAAU,oBAAoB,CAAC,KAAc;IAClD,OAAO,KAAK,YAAY,MAAM,IAAI,aAAa,IAAI,KAAK,CAAC;AAC1D,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,QAAQ;IACQ;IAA5B,YAA4B,gBAAwB,EAAE;QAA1B,kBAAa,GAAb,aAAa,CAAa;IAAG,CAAC;IAE1D,IAAI,KAAK;QACR,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,OAAO;QACV,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC;IAClE,CAAC;IAED,CAAC,aAAa,CAAC;QACd,OAAO,sCAAsC,IAAI,CAAC,aAAa,GAAG,CAAC;IACpE,CAAC;CACD;AAED;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAGtB;IAFjB,YACC,OAAe,EACC,IAAY,EAC5B,IAAoB;QAEpB,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAHL,SAAI,GAAJ,IAAI,CAAQ;QAK5B,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ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AI,CAAC;IACxB,CAAC;IAED,MAAM,CAAC,EAAE,CAAC,KAAc;QACvB,OAAO,KAAK,YAAY,YAAY,CAAC;IACtC,CAAC;IAEQ,CAAC,aAAa,CAAC;QACvB,OAAO,SAAS,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,aAAa,IAAI,CAAC,EAAE,eAAe,IAAI,CAAC,GAAG,EAAE,CAAC;IAC/F,CAAC;CACD"}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { ErrorWithCode } from './base.js';
|
|
1
|
+
import { ErrorWithCode, type IErrorOptions } from './base.js';
|
|
2
2
|
export declare abstract class ProgramError extends ErrorWithCode {
|
|
3
|
-
constructor(message: string,
|
|
3
|
+
constructor(message: string, opts?: IErrorOptions);
|
|
4
4
|
}
|
|
5
5
|
export declare class NotImplementedError extends ProgramError {
|
|
6
|
-
constructor(message: string, boundary?: CallableFunction);
|
|
7
6
|
}
|
|
8
7
|
export declare class SoftwareDefectError extends ProgramError {
|
|
9
|
-
constructor(message: string, boundary?: CallableFunction);
|
|
10
8
|
}
|
|
11
9
|
export declare class Assertion extends SoftwareDefectError {
|
|
12
|
-
static ok(value: unknown, message?: string,
|
|
10
|
+
static ok(value: unknown, message?: string, opts?: IErrorOptions): asserts value;
|
|
13
11
|
}
|
|
14
12
|
//# sourceMappingURL=development.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"development.d.ts","sourceRoot":"","sources":["../../src/common/development.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"development.d.ts","sourceRoot":"","sources":["../../src/common/development.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAE9D,8BAAsB,YAAa,SAAQ,aAAa;gBAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa;CAGjD;AAED,qBAAa,mBAAoB,SAAQ,YAAY;CAAG;AAExD,qBAAa,mBAAoB,SAAQ,YAAY;CAAG;AAExD,qBAAa,SAAU,SAAQ,mBAAmB;IACjD,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,GAAE,MAA2B,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,KAAK;CASpG"}
|
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import { ExitCode } from '../codes/wellknown-exit-codes.js';
|
|
2
2
|
import { ErrorWithCode } from './base.js';
|
|
3
3
|
export class ProgramError extends ErrorWithCode {
|
|
4
|
-
constructor(message,
|
|
5
|
-
super(message, ExitCode.PROGRAM,
|
|
4
|
+
constructor(message, opts) {
|
|
5
|
+
super(message, ExitCode.PROGRAM, opts);
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
export class NotImplementedError extends ProgramError {
|
|
9
|
-
constructor(message, boundary) {
|
|
10
|
-
super(message, boundary);
|
|
11
|
-
this.name = 'NotImplemented';
|
|
12
|
-
}
|
|
13
9
|
}
|
|
14
10
|
export class SoftwareDefectError extends ProgramError {
|
|
15
|
-
constructor(message, boundary) {
|
|
16
|
-
super(message, boundary);
|
|
17
|
-
this.name = 'SoftwareDefect';
|
|
18
|
-
}
|
|
19
11
|
}
|
|
20
12
|
export class Assertion extends SoftwareDefectError {
|
|
21
|
-
static ok(value, message = 'Assertion failed',
|
|
13
|
+
static ok(value, message = 'Assertion failed', opts) {
|
|
22
14
|
if (!value) {
|
|
23
|
-
|
|
15
|
+
if (!opts?.boundary) {
|
|
16
|
+
if (!opts)
|
|
17
|
+
opts = {};
|
|
18
|
+
opts.boundary = Assertion.ok;
|
|
19
|
+
}
|
|
20
|
+
throw new SoftwareDefectError(`${message}: value should be truthy, got ${typeof value} (${value})`, opts);
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"development.js","sourceRoot":"","sources":["../../src/common/development.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"development.js","sourceRoot":"","sources":["../../src/common/development.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAsB,MAAM,WAAW,CAAC;AAE9D,MAAM,OAAgB,YAAa,SAAQ,aAAa;IACvD,YAAY,OAAe,EAAE,IAAoB;QAChD,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACxC,CAAC;CACD;AAED,MAAM,OAAO,mBAAoB,SAAQ,YAAY;CAAG;AAExD,MAAM,OAAO,mBAAoB,SAAQ,YAAY;CAAG;AAExD,MAAM,OAAO,SAAU,SAAQ,mBAAmB;IACjD,MAAM,CAAC,EAAE,CAAC,KAAc,EAAE,UAAkB,kBAAkB,EAAE,IAAoB;QACnF,IAAI,CAAC,KAAK,EAAE,CAAC;YACZ,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC;gBACrB,IAAI,CAAC,IAAI;oBAAE,IAAI,GAAG,EAAE,CAAC;gBACrB,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC,EAAE,CAAC;YAC9B,CAAC;YACD,MAAM,IAAI,mBAAmB,CAAC,GAAG,OAAO,iCAAiC,OAAO,KAAK,KAAK,KAAK,GAAG,EAAE,IAAI,CAAC,CAAC;QAC3G,CAAC;IACF,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@idlebox/errors",
|
|
3
3
|
"description": "A collection of my errors.",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "0.1.
|
|
5
|
+
"version": "0.1.14",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"source": "./src/autoindex.ts",
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
},
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@idlebox/node-error-codes": "^0.1.
|
|
15
|
+
"@idlebox/node-error-codes": "^0.1.13"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@build-script/baseline-rig": "latest",
|
|
19
|
-
"@mpis/run": "^0.0.
|
|
19
|
+
"@mpis/run": "^0.0.19"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "GongT <admin@gongt.me>",
|
package/src/autoindex.ts
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
export { UsageError } from "./common/application.js";
|
|
30
30
|
/* common/base.ts */
|
|
31
31
|
// Identifiers
|
|
32
|
+
export type { IErrorOptions } from "./common/base.js";
|
|
32
33
|
export { humanReadable } from "./common/base.js";
|
|
33
34
|
export type { IHumanReadable } from "./common/base.js";
|
|
34
35
|
export { isHumanReadableError } from "./common/base.js";
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ExitCode } from '../codes/wellknown-exit-codes.js';
|
|
2
|
-
import { ErrorWithCode, humanReadable } from './base.js';
|
|
2
|
+
import { ErrorWithCode, humanReadable, type IErrorOptions } from './base.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 程序因为正常运行结束而退出
|
|
6
6
|
* catch到此错误时应直接重新抛出,不应做其他处理
|
|
7
7
|
*/
|
|
8
8
|
export class Exit extends ErrorWithCode {
|
|
9
|
-
constructor(code: number,
|
|
10
|
-
super(`process exit with code ${code}`, code,
|
|
9
|
+
constructor(code: number, opts?: IErrorOptions) {
|
|
10
|
+
super(`process exit with code ${code}`, code, opts);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -18,9 +18,9 @@ export class Exit extends ErrorWithCode {
|
|
|
18
18
|
export class InterruptError extends ErrorWithCode {
|
|
19
19
|
constructor(
|
|
20
20
|
public readonly signal: NodeJS.Signals,
|
|
21
|
-
|
|
21
|
+
opts?: IErrorOptions,
|
|
22
22
|
) {
|
|
23
|
-
super(`interrupt by signal ${signal}`, ExitCode.INTERRUPT,
|
|
23
|
+
super(`interrupt by signal ${signal}`, ExitCode.INTERRUPT, opts);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
override [humanReadable]() {
|
|
@@ -32,8 +32,8 @@ export class InterruptError extends ErrorWithCode {
|
|
|
32
32
|
* 由于错误的参数、配置导致错误
|
|
33
33
|
*/
|
|
34
34
|
export class UsageError extends ErrorWithCode {
|
|
35
|
-
constructor(message: string,
|
|
36
|
-
super(message, ExitCode.USAGE,
|
|
35
|
+
constructor(message: string, opts?: IErrorOptions) {
|
|
36
|
+
super(message, ExitCode.USAGE, opts);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
override [humanReadable]() {
|
package/src/common/base.ts
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
import { ExitCode } from '../codes/wellknown-exit-codes.js';
|
|
2
|
+
|
|
1
3
|
function noop() {}
|
|
2
4
|
|
|
3
5
|
const replaceStackTrace = Error.captureStackTrace ?? noop;
|
|
4
6
|
|
|
7
|
+
export interface IErrorOptions {
|
|
8
|
+
/**
|
|
9
|
+
* stack属性的边界函数
|
|
10
|
+
* @see {Error.captureStackTrace}
|
|
11
|
+
*/
|
|
12
|
+
boundary?: CallableFunction;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* cause属性的值
|
|
16
|
+
* @see {Error.cause}
|
|
17
|
+
*/
|
|
18
|
+
cause?: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
5
21
|
export const humanReadable = Symbol('humanReadable');
|
|
6
22
|
export interface IHumanReadable {
|
|
7
23
|
[humanReadable](): string;
|
|
@@ -11,38 +27,40 @@ export function isHumanReadableError(error: unknown): error is IHumanReadable {
|
|
|
11
27
|
return error instanceof Object && humanReadable in error;
|
|
12
28
|
}
|
|
13
29
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 利用try...catch统一接口,或是用于分支条件等,未发生错误的情况
|
|
32
|
+
*/
|
|
33
|
+
export class NotError implements IHumanReadable {
|
|
34
|
+
constructor(public readonly extra_message: string = '') {}
|
|
35
|
+
|
|
36
|
+
get stack() {
|
|
37
|
+
throw new Error(`NotError 未被正确捕获 [hint: ${this.extra_message}]`);
|
|
17
38
|
}
|
|
18
|
-
|
|
19
|
-
|
|
39
|
+
get message() {
|
|
40
|
+
throw new Error(`NotError 未被正确捕获 [hint: ${this.extra_message}]`);
|
|
20
41
|
}
|
|
21
42
|
|
|
22
43
|
[humanReadable]() {
|
|
23
|
-
return
|
|
44
|
+
return `你不应该看到此消息。你无需尝试处理该错误,请联系开发者。[hint: ${this.extra_message}]`;
|
|
24
45
|
}
|
|
25
46
|
}
|
|
26
47
|
|
|
27
48
|
/**
|
|
28
|
-
* 应用程序退出
|
|
29
|
-
* 此错误通常不需要输出到日志
|
|
30
49
|
* @abstract
|
|
31
50
|
*/
|
|
32
51
|
export class ErrorWithCode extends Error implements IHumanReadable {
|
|
33
52
|
constructor(
|
|
34
53
|
message: string,
|
|
35
54
|
public readonly code: number,
|
|
36
|
-
|
|
55
|
+
opts?: IErrorOptions,
|
|
37
56
|
) {
|
|
38
|
-
|
|
39
|
-
Error.stackTraceLimit = 0;
|
|
40
|
-
super(message);
|
|
41
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
57
|
+
super(message, opts);
|
|
42
58
|
|
|
43
|
-
this
|
|
59
|
+
replaceStackTrace(this, opts?.boundary ?? this.constructor);
|
|
60
|
+
}
|
|
44
61
|
|
|
45
|
-
|
|
62
|
+
public override get name() {
|
|
63
|
+
return this.constructor.name;
|
|
46
64
|
}
|
|
47
65
|
|
|
48
66
|
[humanReadable]() {
|
|
@@ -56,20 +74,22 @@ export class ErrorWithCode extends Error implements IHumanReadable {
|
|
|
56
74
|
}
|
|
57
75
|
}
|
|
58
76
|
|
|
77
|
+
Object.setPrototypeOf(ErrorWithCode.prototype, Error.prototype);
|
|
78
|
+
|
|
59
79
|
/**
|
|
60
80
|
* Error when something.cancel() is called
|
|
61
81
|
* @public
|
|
62
82
|
*/
|
|
63
|
-
export class CanceledError extends
|
|
64
|
-
constructor() {
|
|
65
|
-
super('Canceled');
|
|
83
|
+
export class CanceledError extends ErrorWithCode implements IHumanReadable {
|
|
84
|
+
constructor(opts?: IErrorOptions) {
|
|
85
|
+
super('Canceled', ExitCode.INTERRUPT, opts);
|
|
66
86
|
}
|
|
67
87
|
|
|
68
88
|
static is(e: unknown): e is CanceledError {
|
|
69
89
|
return e instanceof CanceledError;
|
|
70
90
|
}
|
|
71
91
|
|
|
72
|
-
[humanReadable]() {
|
|
92
|
+
override [humanReadable]() {
|
|
73
93
|
return '操作已应用户要求而取消';
|
|
74
94
|
}
|
|
75
95
|
}
|
|
@@ -78,20 +98,23 @@ export class CanceledError extends Error implements IHumanReadable {
|
|
|
78
98
|
* Error when something timeout
|
|
79
99
|
* @public
|
|
80
100
|
*/
|
|
81
|
-
export class TimeoutError extends
|
|
101
|
+
export class TimeoutError extends ErrorWithCode implements IHumanReadable {
|
|
102
|
+
private readonly what?: string;
|
|
103
|
+
|
|
82
104
|
constructor(
|
|
83
105
|
private readonly ms: number,
|
|
84
106
|
private readonly why = 'no response',
|
|
85
|
-
|
|
107
|
+
opts?: IErrorOptions & { what?: string },
|
|
86
108
|
) {
|
|
87
|
-
super(`Timeout: ${why} in ${ms}ms${what ? ` (when ${what})` : ''}
|
|
109
|
+
super(`Timeout: ${why} in ${ms}ms${opts?.what ? ` (when ${opts.what})` : ''}`, ExitCode.TIMEOUT, opts);
|
|
110
|
+
this.what = opts?.what;
|
|
88
111
|
}
|
|
89
112
|
|
|
90
113
|
static is(error: unknown): error is TimeoutError {
|
|
91
114
|
return error instanceof TimeoutError;
|
|
92
115
|
}
|
|
93
116
|
|
|
94
|
-
[humanReadable]() {
|
|
117
|
+
override [humanReadable]() {
|
|
95
118
|
return `操作超时: ${this.what ? this.what : '操作没有说明'}\n - 时长: ${this.ms}ms\n - 原因: ${this.why}`;
|
|
96
119
|
}
|
|
97
120
|
}
|
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
import { ExitCode } from '../codes/wellknown-exit-codes.js';
|
|
2
|
-
import { ErrorWithCode } from './base.js';
|
|
2
|
+
import { ErrorWithCode, type IErrorOptions } from './base.js';
|
|
3
3
|
|
|
4
4
|
export abstract class ProgramError extends ErrorWithCode {
|
|
5
|
-
constructor(message: string,
|
|
6
|
-
super(message, ExitCode.PROGRAM,
|
|
5
|
+
constructor(message: string, opts?: IErrorOptions) {
|
|
6
|
+
super(message, ExitCode.PROGRAM, opts);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export class NotImplementedError extends ProgramError {
|
|
11
|
-
constructor(message: string, boundary?: CallableFunction) {
|
|
12
|
-
super(message, boundary);
|
|
13
|
-
this.name = 'NotImplemented';
|
|
14
|
-
}
|
|
15
|
-
}
|
|
10
|
+
export class NotImplementedError extends ProgramError {}
|
|
16
11
|
|
|
17
|
-
export class SoftwareDefectError extends ProgramError {
|
|
18
|
-
constructor(message: string, boundary?: CallableFunction) {
|
|
19
|
-
super(message, boundary);
|
|
20
|
-
this.name = 'SoftwareDefect';
|
|
21
|
-
}
|
|
22
|
-
}
|
|
12
|
+
export class SoftwareDefectError extends ProgramError {}
|
|
23
13
|
|
|
24
14
|
export class Assertion extends SoftwareDefectError {
|
|
25
|
-
static ok(value: unknown, message: string = 'Assertion failed',
|
|
15
|
+
static ok(value: unknown, message: string = 'Assertion failed', opts?: IErrorOptions): asserts value {
|
|
26
16
|
if (!value) {
|
|
27
|
-
|
|
17
|
+
if (!opts?.boundary) {
|
|
18
|
+
if (!opts) opts = {};
|
|
19
|
+
opts.boundary = Assertion.ok;
|
|
20
|
+
}
|
|
21
|
+
throw new SoftwareDefectError(`${message}: value should be truthy, got ${typeof value} (${value})`, opts);
|
|
28
22
|
}
|
|
29
23
|
}
|
|
30
24
|
}
|