@taicode/common-base 1.4.0 → 1.5.1

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.
@@ -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 {
@@ -49,20 +61,20 @@ type SystemErrorType = keyof SystemErrorTypes extends never ? string : Extract<k
49
61
  type UserErrorContext<T extends string> = T extends keyof UserErrorTypes ? UserErrorTypes[T] & ErrorContext : ErrorContext;
50
62
  type SystemErrorContext<T extends string> = T extends keyof SystemErrorTypes ? SystemErrorTypes[T] & ErrorContext : ErrorContext;
51
63
  /** 用户可见的错误 */
52
- export declare class UserError<T extends string = UserErrorType> extends Error {
64
+ export declare class UserError<T extends UserErrorType = UserErrorType> extends Error {
53
65
  type: T;
54
66
  message: string;
55
67
  context?: UserErrorContext<T> | undefined;
56
- constructor(type: T, message: string, context?: UserErrorContext<T> | undefined);
57
- static is<T extends string = UserErrorType>(error: unknown): error is UserError<T>;
68
+ constructor(type: T, message?: string, context?: UserErrorContext<T> | undefined);
69
+ static is<T extends UserErrorType = UserErrorType>(error: unknown): error is UserError<T>;
58
70
  }
59
71
  /** 系统错误 */
60
- export declare class SystemError<T extends string = SystemErrorType> extends Error {
72
+ export declare class SystemError<T extends SystemErrorType = SystemErrorType> extends Error {
61
73
  type: T;
62
74
  message: string;
63
75
  context?: SystemErrorContext<T> | undefined;
64
- constructor(type: T, message: string, context?: SystemErrorContext<T> | undefined);
65
- static is<T extends string = SystemErrorType>(error: unknown): error is SystemError<T>;
76
+ constructor(type: T, message?: string, context?: SystemErrorContext<T> | undefined);
77
+ static is<T extends SystemErrorType = SystemErrorType>(error: unknown): error is SystemError<T>;
66
78
  }
67
79
  export declare class UnknownError extends Error {
68
80
  context?: ErrorContext | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../source/error/error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;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;IAAS,OAAO,CAAC,EAAE,gBAAgB,CA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K,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAAC,CAAC,CAAC;CAGhG;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"}
@@ -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;
@@ -1,2 +1,19 @@
1
+ declare module './error' {
2
+ interface UserErrorTypes {
3
+ VALIDATION_ERROR: unknown;
4
+ TEST_ERROR: unknown;
5
+ USER_ERROR: unknown;
6
+ REQUIRED_FIELD: unknown;
7
+ INVALID_FORMAT: unknown;
8
+ OUT_OF_RANGE: unknown;
9
+ }
10
+ interface SystemErrorTypes {
11
+ DATABASE_ERROR: unknown;
12
+ NETWORK_ERROR: unknown;
13
+ FILE_ERROR: unknown;
14
+ TEST_ERROR: unknown;
15
+ SYSTEM_ERROR: unknown;
16
+ }
17
+ }
1
18
  export {};
2
19
  //# sourceMappingURL=error.test.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.test.d.ts","sourceRoot":"","sources":["../../source/error/error.test.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"error.test.d.ts","sourceRoot":"","sources":["../../source/error/error.test.ts"],"names":[],"mappings":"AAGA,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,gBAAgB,EAAE,OAAO,CAAA;QACzB,UAAU,EAAE,OAAO,CAAA;QACnB,UAAU,EAAE,OAAO,CAAA;QACnB,cAAc,EAAE,OAAO,CAAA;QACvB,cAAc,EAAE,OAAO,CAAA;QACvB,YAAY,EAAE,OAAO,CAAA;KACtB;IAED,UAAU,gBAAgB;QACxB,cAAc,EAAE,OAAO,CAAA;QACvB,aAAa,EAAE,OAAO,CAAA;QACtB,UAAU,EAAE,OAAO,CAAA;QACnB,UAAU,EAAE,OAAO,CAAA;QACnB,YAAY,EAAE,OAAO,CAAA;KACtB;CACF"}
@@ -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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taicode/common-base",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "author": "Alain",
5
5
  "license": "ISC",
6
6
  "description": "",
@@ -21,6 +21,7 @@
21
21
  "scripts": {
22
22
  "test": "vitest run",
23
23
  "build": "tsc -p tsconfig.json",
24
+ "prepublishOnly": "npm run build",
24
25
  "dev": "tsc -p tsconfig.json --watch"
25
26
  },
26
27
  "files": [