@taicode/common-base 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/output/error/error.d.ts
CHANGED
|
@@ -29,11 +29,23 @@
|
|
|
29
29
|
* value: 'invalid-email',
|
|
30
30
|
* rule: 'email-format'
|
|
31
31
|
* })
|
|
32
|
+
* // 或者省略 message,直接使用 type 作为消息
|
|
33
|
+
* const userError2 = new UserError('validation-failed', undefined, {
|
|
34
|
+
* field: 'email',
|
|
35
|
+
* value: 'invalid-email',
|
|
36
|
+
* rule: 'email-format'
|
|
37
|
+
* })
|
|
32
38
|
* const systemError = new SystemError('database-error', '数据库连接失败', {
|
|
33
39
|
* table: 'users',
|
|
34
40
|
* query: 'SELECT * FROM users',
|
|
35
41
|
* operation: 'select'
|
|
36
42
|
* })
|
|
43
|
+
* // 或者省略 message
|
|
44
|
+
* const systemError2 = new SystemError('database-error', undefined, {
|
|
45
|
+
* table: 'users',
|
|
46
|
+
* query: 'SELECT * FROM users',
|
|
47
|
+
* operation: 'select'
|
|
48
|
+
* })
|
|
37
49
|
* ```
|
|
38
50
|
*/
|
|
39
51
|
interface ErrorContext {
|
|
@@ -53,7 +65,7 @@ export declare class UserError<T extends string = UserErrorType> extends Error {
|
|
|
53
65
|
type: T;
|
|
54
66
|
message: string;
|
|
55
67
|
context?: UserErrorContext<T> | undefined;
|
|
56
|
-
constructor(type: T, message
|
|
68
|
+
constructor(type: T, message?: string, context?: UserErrorContext<T> | undefined);
|
|
57
69
|
static is<T extends string = UserErrorType>(error: unknown): error is UserError<T>;
|
|
58
70
|
}
|
|
59
71
|
/** 系统错误 */
|
|
@@ -61,7 +73,7 @@ export declare class SystemError<T extends string = SystemErrorType> extends Err
|
|
|
61
73
|
type: T;
|
|
62
74
|
message: string;
|
|
63
75
|
context?: SystemErrorContext<T> | undefined;
|
|
64
|
-
constructor(type: T, message
|
|
76
|
+
constructor(type: T, message?: string, context?: SystemErrorContext<T> | undefined);
|
|
65
77
|
static is<T extends string = SystemErrorType>(error: unknown): error is SystemError<T>;
|
|
66
78
|
}
|
|
67
79
|
export declare class UnknownError extends Error {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../source/error/error.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../source/error/error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,UAAU,YAAY;IACpB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAID,MAAM,WAAW,cAAc;CAAG;AAClC,MAAM,WAAW,gBAAgB;CAAG;AAGpC,KAAK,aAAa,GAAG,MAAM,cAAc,SAAS,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,cAAc,EAAE,MAAM,CAAC,CAAA;AACxG,KAAK,eAAe,GAAG,MAAM,gBAAgB,SAAS,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,gBAAgB,EAAE,MAAM,CAAC,CAAA;AAG9G,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,MAAM,cAAc,GACpE,cAAc,CAAC,CAAC,CAAC,GAAG,YAAY,GAChC,YAAY,CAAA;AAEhB,KAAK,kBAAkB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,MAAM,gBAAgB,GACxE,gBAAgB,CAAC,CAAC,CAAC,GAAG,YAAY,GAClC,YAAY,CAAA;AAEhB,cAAc;AACd,qBAAa,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,aAAa,CAAE,SAAQ,KAAK;IACjD,IAAI,EAAE,CAAC;IAAS,OAAO,EAAE,MAAM;IAAc,OAAO,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;gBAA1E,IAAI,EAAE,CAAC,EAAS,OAAO,GAAE,MAAW,EAAS,OAAO,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,YAAA;IAK7F,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,MAAM,GAAG,aAAa,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC;CAGnF;AAED,WAAW;AACX,qBAAa,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,eAAe,CAAE,SAAQ,KAAK;IACrD,IAAI,EAAE,CAAC;IAAS,OAAO,EAAE,MAAM;IAAc,OAAO,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC;gBAA5E,IAAI,EAAE,CAAC,EAAS,OAAO,GAAE,MAAW,EAAS,OAAO,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,YAAA;IAK/F,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,MAAM,GAAG,eAAe,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAAC,CAAC,CAAC;CAGvF;AAED,qBAAa,YAAa,SAAQ,KAAK;IACD,OAAO,CAAC,EAAE,YAAY;gBAA9C,OAAO,EAAE,MAAM,EAAS,OAAO,CAAC,EAAE,YAAY,YAAA;IAK1D,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY;CAGjD"}
|
package/output/error/error.js
CHANGED
|
@@ -29,11 +29,23 @@
|
|
|
29
29
|
* value: 'invalid-email',
|
|
30
30
|
* rule: 'email-format'
|
|
31
31
|
* })
|
|
32
|
+
* // 或者省略 message,直接使用 type 作为消息
|
|
33
|
+
* const userError2 = new UserError('validation-failed', undefined, {
|
|
34
|
+
* field: 'email',
|
|
35
|
+
* value: 'invalid-email',
|
|
36
|
+
* rule: 'email-format'
|
|
37
|
+
* })
|
|
32
38
|
* const systemError = new SystemError('database-error', '数据库连接失败', {
|
|
33
39
|
* table: 'users',
|
|
34
40
|
* query: 'SELECT * FROM users',
|
|
35
41
|
* operation: 'select'
|
|
36
42
|
* })
|
|
43
|
+
* // 或者省略 message
|
|
44
|
+
* const systemError2 = new SystemError('database-error', undefined, {
|
|
45
|
+
* table: 'users',
|
|
46
|
+
* query: 'SELECT * FROM users',
|
|
47
|
+
* operation: 'select'
|
|
48
|
+
* })
|
|
37
49
|
* ```
|
|
38
50
|
*/
|
|
39
51
|
/** 用户可见的错误 */
|
|
@@ -41,7 +53,7 @@ export class UserError extends Error {
|
|
|
41
53
|
type;
|
|
42
54
|
message;
|
|
43
55
|
context;
|
|
44
|
-
constructor(type, message, context) {
|
|
56
|
+
constructor(type, message = '', context) {
|
|
45
57
|
super(message, context);
|
|
46
58
|
this.type = type;
|
|
47
59
|
this.message = message;
|
|
@@ -57,7 +69,7 @@ export class SystemError extends Error {
|
|
|
57
69
|
type;
|
|
58
70
|
message;
|
|
59
71
|
context;
|
|
60
|
-
constructor(type, message, context) {
|
|
72
|
+
constructor(type, message = '', context) {
|
|
61
73
|
super(message, context);
|
|
62
74
|
this.type = type;
|
|
63
75
|
this.message = message;
|
|
@@ -12,6 +12,33 @@ describe('UserError', () => {
|
|
|
12
12
|
expect(error.type).toBe(type);
|
|
13
13
|
expect(error.message).toBe(message);
|
|
14
14
|
});
|
|
15
|
+
it('应该在未提供message时使用空字符串作为默认message', () => {
|
|
16
|
+
const type = 'VALIDATION_ERROR';
|
|
17
|
+
const error = new UserError(type);
|
|
18
|
+
expect(error).toBeInstanceOf(Error);
|
|
19
|
+
expect(error).toBeInstanceOf(UserError);
|
|
20
|
+
expect(error.name).toBe('UserError');
|
|
21
|
+
expect(error.type).toBe(type);
|
|
22
|
+
expect(error.message).toBe('');
|
|
23
|
+
});
|
|
24
|
+
it('应该在提供undefined message时使用空字符串作为默认message', () => {
|
|
25
|
+
const type = 'VALIDATION_ERROR';
|
|
26
|
+
const error = new UserError(type, undefined);
|
|
27
|
+
expect(error).toBeInstanceOf(Error);
|
|
28
|
+
expect(error).toBeInstanceOf(UserError);
|
|
29
|
+
expect(error.name).toBe('UserError');
|
|
30
|
+
expect(error.type).toBe(type);
|
|
31
|
+
expect(error.message).toBe('');
|
|
32
|
+
});
|
|
33
|
+
it('应该在提供空字符串message时保持空字符串', () => {
|
|
34
|
+
const type = 'VALIDATION_ERROR';
|
|
35
|
+
const error = new UserError(type, '');
|
|
36
|
+
expect(error).toBeInstanceOf(Error);
|
|
37
|
+
expect(error).toBeInstanceOf(UserError);
|
|
38
|
+
expect(error.name).toBe('UserError');
|
|
39
|
+
expect(error.type).toBe(type);
|
|
40
|
+
expect(error.message).toBe('');
|
|
41
|
+
});
|
|
15
42
|
it('应该支持ErrorOptions参数', () => {
|
|
16
43
|
const type = 'VALIDATION_ERROR';
|
|
17
44
|
const message = '输入验证失败';
|
|
@@ -19,6 +46,13 @@ describe('UserError', () => {
|
|
|
19
46
|
const error = new UserError(type, message, { cause });
|
|
20
47
|
expect(error.cause).toBe(cause);
|
|
21
48
|
});
|
|
49
|
+
it('应该支持ErrorOptions参数且message为可选', () => {
|
|
50
|
+
const type = 'VALIDATION_ERROR';
|
|
51
|
+
const cause = new Error('原始错误');
|
|
52
|
+
const error = new UserError(type, undefined, { cause });
|
|
53
|
+
expect(error.cause).toBe(cause);
|
|
54
|
+
expect(error.message).toBe('');
|
|
55
|
+
});
|
|
22
56
|
it('应该正确设置错误堆栈信息', () => {
|
|
23
57
|
const error = new UserError('TEST_ERROR', '测试错误');
|
|
24
58
|
expect(error.stack).toBeDefined();
|
|
@@ -84,6 +118,33 @@ describe('SystemError', () => {
|
|
|
84
118
|
expect(error.type).toBe(type);
|
|
85
119
|
expect(error.message).toBe(message);
|
|
86
120
|
});
|
|
121
|
+
it('应该在未提供message时使用空字符串作为默认message', () => {
|
|
122
|
+
const type = 'DATABASE_ERROR';
|
|
123
|
+
const error = new SystemError(type);
|
|
124
|
+
expect(error).toBeInstanceOf(Error);
|
|
125
|
+
expect(error).toBeInstanceOf(SystemError);
|
|
126
|
+
expect(error.name).toBe('SystemError');
|
|
127
|
+
expect(error.type).toBe(type);
|
|
128
|
+
expect(error.message).toBe('');
|
|
129
|
+
});
|
|
130
|
+
it('应该在提供undefined message时使用空字符串作为默认message', () => {
|
|
131
|
+
const type = 'DATABASE_ERROR';
|
|
132
|
+
const error = new SystemError(type, undefined);
|
|
133
|
+
expect(error).toBeInstanceOf(Error);
|
|
134
|
+
expect(error).toBeInstanceOf(SystemError);
|
|
135
|
+
expect(error.name).toBe('SystemError');
|
|
136
|
+
expect(error.type).toBe(type);
|
|
137
|
+
expect(error.message).toBe('');
|
|
138
|
+
});
|
|
139
|
+
it('应该在提供空字符串message时保持空字符串', () => {
|
|
140
|
+
const type = 'DATABASE_ERROR';
|
|
141
|
+
const error = new SystemError(type, '');
|
|
142
|
+
expect(error).toBeInstanceOf(Error);
|
|
143
|
+
expect(error).toBeInstanceOf(SystemError);
|
|
144
|
+
expect(error.name).toBe('SystemError');
|
|
145
|
+
expect(error.type).toBe(type);
|
|
146
|
+
expect(error.message).toBe('');
|
|
147
|
+
});
|
|
87
148
|
it('应该支持ErrorOptions参数', () => {
|
|
88
149
|
const type = 'NETWORK_ERROR';
|
|
89
150
|
const message = '网络请求失败';
|
|
@@ -91,6 +152,13 @@ describe('SystemError', () => {
|
|
|
91
152
|
const error = new SystemError(type, message, { cause });
|
|
92
153
|
expect(error.cause).toBe(cause);
|
|
93
154
|
});
|
|
155
|
+
it('应该支持ErrorOptions参数且message为可选', () => {
|
|
156
|
+
const type = 'NETWORK_ERROR';
|
|
157
|
+
const cause = new Error('连接超时');
|
|
158
|
+
const error = new SystemError(type, undefined, { cause });
|
|
159
|
+
expect(error.cause).toBe(cause);
|
|
160
|
+
expect(error.message).toBe('');
|
|
161
|
+
});
|
|
94
162
|
it('应该正确设置错误堆栈信息', () => {
|
|
95
163
|
const error = new SystemError('FILE_ERROR', '文件读取失败');
|
|
96
164
|
expect(error.stack).toBeDefined();
|