@longzai-intelligence/error-core 0.0.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.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # @longzai-intelligence/error-core
2
+
3
+ 领域错误的核心异常类包,提供统一的错误处理机制。
4
+
5
+ ## 概述
6
+
7
+ 本包提供了领域驱动设计的核心异常类,包括:
8
+
9
+ - **DomainError**: 领域错误抽象基类
10
+ - **ValidationError**: 验证错误
11
+ - **EntityNotFoundError**: 实体未找到错误
12
+ - **RepositoryError**: 仓储操作错误
13
+ - **PermissionDeniedError**: 权限拒绝错误
14
+ - **BusinessRuleError**: 业务规则错误
15
+ - **ConcurrencyError**: 并发冲突错误
16
+ - **ConfigurationError**: 配置错误
17
+ - **ExternalServiceError**: 外部服务错误
18
+ - **DuplicateEntityError**: 实体重复错误
19
+ - **AuthenticationError**: 认证错误
20
+ - **NotFoundError**: 资源未找到错误
21
+
22
+ ## 安装
23
+
24
+ ```bash
25
+ bun add @longzai-intelligence/error-core
26
+ ```
27
+
28
+ ## 核心概念
29
+
30
+ ### DomainError
31
+
32
+ 所有领域异常的抽象基类,提供统一的错误处理接口。
33
+
34
+ 特性:
35
+ - 错误代码:用于程序化识别错误类型
36
+ - 上下文信息:附加与错误相关的额外数据
37
+ - JSON 序列化:支持日志记录和 API 响应
38
+
39
+ ### 错误分类
40
+
41
+ | 错误类 | 用途 |
42
+ |--------|------|
43
+ | ValidationError | 输入验证失败、业务规则校验失败 |
44
+ | EntityNotFoundError | 实体在数据库中不存在 |
45
+ | RepositoryError | 数据库操作失败、持久化层错误 |
46
+ | PermissionDeniedError | 权限不足、操作被拒绝 |
47
+ | BusinessRuleError | 业务规则违反 |
48
+ | ConcurrencyError | 并发冲突、乐观锁失败 |
49
+ | ConfigurationError | 配置缺失或无效 |
50
+ | ExternalServiceError | 外部服务调用失败 |
51
+ | DuplicateEntityError | 实体已存在、唯一约束冲突 |
52
+ | AuthenticationError | 认证失败、会话过期 |
53
+ | NotFoundError | 通用资源未找到 |
54
+
55
+ ## 使用示例
56
+
57
+ ### 导入错误类
58
+
59
+ ```typescript
60
+ import {
61
+ DomainError,
62
+ ValidationError,
63
+ EntityNotFoundError,
64
+ RepositoryError,
65
+ PermissionDeniedError,
66
+ } from '@longzai-intelligence/error-core';
67
+ ```
68
+
69
+ ### 使用工厂函数
70
+
71
+ ```typescript
72
+ import {
73
+ createValidationError,
74
+ createEntityNotFoundError,
75
+ createRepositoryError,
76
+ createPermissionDeniedError,
77
+ } from '@longzai-intelligence/error-core';
78
+
79
+ // 验证失败
80
+ throw createValidationError('用户名格式不正确', 'username', username);
81
+
82
+ // 实体不存在
83
+ throw createEntityNotFoundError('用户', userId);
84
+
85
+ // 仓储操作失败
86
+ throw createRepositoryError('获取用户列表失败', error);
87
+
88
+ // 权限不足
89
+ throw createPermissionDeniedError('包', '发布');
90
+ ```
91
+
92
+ ### 自定义领域错误
93
+
94
+ ```typescript
95
+ import { DomainError } from '@longzai-intelligence/error-core';
96
+
97
+ class InsufficientBalanceError extends DomainError {
98
+ constructor(userId: string, balance: number, required: number) {
99
+ super(
100
+ `余额不足: 当前 ${balance}, 需要 ${required}`,
101
+ 'INSUFFICIENT_BALANCE',
102
+ { userId, balance, required }
103
+ );
104
+ }
105
+ }
106
+ ```
107
+
108
+ ### 错误序列化
109
+
110
+ ```typescript
111
+ try {
112
+ // 业务操作
113
+ } catch (error) {
114
+ if (error instanceof DomainError) {
115
+ console.log(error.toJSON());
116
+ // {
117
+ // name: 'ValidationError',
118
+ // code: 'VALIDATION_ERROR',
119
+ // message: '用户名格式不正确',
120
+ // context: { field: 'username', value: 'abc' },
121
+ // stack: '...'
122
+ // }
123
+ }
124
+ }
125
+ ```
126
+
127
+ ## 设计原则
128
+
129
+ - **类型安全**: 所有错误类都继承自 DomainError 基类
130
+ - **上下文丰富**: 错误携带详细的上下文信息
131
+ - **可序列化**: 支持转换为 JSON 用于日志和 API 响应
132
+ - **语义清晰**: 错误类名直接表达错误含义
133
+
134
+ ## 相关包
135
+
136
+ - `@longzai-intelligence/error-utils`: 错误处理工具函数
137
+
138
+ ## 许可证
139
+
140
+ UNLICENSED
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},t=class extends e{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},n=class extends e{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},r=class extends e{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},i=class extends e{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},a=class extends e{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},o=class extends e{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},s=class extends e{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},c=class extends e{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}},l=class extends e{constructor(e,t){super(`${e} 已存在: ${t}`,`DUPLICATE_ENTITY`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`DuplicateEntityError`}},u=class extends e{constructor(e){super(e,`AUTHENTICATION_ERROR`),this.name=`AuthenticationError`}},d=class extends e{constructor(e){super(e,`NOT_FOUND`),this.name=`NotFoundError`}};function f(e,n,r){return new t(e,n,r)}function p(e,t){return new n(e,t)}function m(e,t){return new r(e,t)}function h(e,t){return new i(e,t)}function g(e,t){return new l(e,t)}function _(e){return new u(e)}function v(e){return new d(e)}exports.AuthenticationError=u,exports.BusinessRuleError=a,exports.ConcurrencyError=o,exports.ConfigurationError=s,exports.DomainError=e,exports.DuplicateEntityError=l,exports.EntityNotFoundError=n,exports.ExternalServiceError=c,exports.NotFoundError=d,exports.PermissionDeniedError=i,exports.RepositoryError=r,exports.ValidationError=t,exports.createAuthenticationError=_,exports.createDuplicateEntityError=g,exports.createEntityNotFoundError=p,exports.createNotFoundError=v,exports.createPermissionDeniedError=h,exports.createRepositoryError=m,exports.createValidationError=f;
@@ -0,0 +1,85 @@
1
+ //#region src/domain.errors.d.ts
2
+ declare abstract class DomainError extends Error {
3
+ readonly code: string;
4
+ readonly context?: Record<string, unknown> | undefined;
5
+ constructor(message: string, code?: string, context?: Record<string, unknown> | undefined);
6
+ toJSON(): object;
7
+ }
8
+ //#endregion
9
+ //#region src/validation.errors.d.ts
10
+ declare class ValidationError extends DomainError {
11
+ readonly field?: string | undefined;
12
+ readonly value?: unknown | undefined;
13
+ constructor(message: string, field?: string | undefined, value?: unknown | undefined);
14
+ }
15
+ //#endregion
16
+ //#region src/entity-not-found.errors.d.ts
17
+ declare class EntityNotFoundError extends DomainError {
18
+ readonly entityType: string;
19
+ readonly entityId: string;
20
+ constructor(entityType: string, entityId: string);
21
+ }
22
+ //#endregion
23
+ //#region src/repository.errors.d.ts
24
+ declare class RepositoryError extends DomainError {
25
+ readonly cause?: Error | undefined;
26
+ constructor(message: string, cause?: Error | undefined, context?: Record<string, unknown>);
27
+ toJSON(): object;
28
+ }
29
+ //#endregion
30
+ //#region src/permission-denied.errors.d.ts
31
+ declare class PermissionDeniedError extends DomainError {
32
+ readonly resource: string;
33
+ readonly action: string;
34
+ constructor(resource: string, action: string);
35
+ }
36
+ //#endregion
37
+ //#region src/business-rule.errors.d.ts
38
+ declare class BusinessRuleError extends DomainError {
39
+ constructor(message: string, rule?: string);
40
+ }
41
+ //#endregion
42
+ //#region src/concurrency.errors.d.ts
43
+ declare class ConcurrencyError extends DomainError {
44
+ constructor(message?: string);
45
+ }
46
+ //#endregion
47
+ //#region src/configuration.errors.d.ts
48
+ declare class ConfigurationError extends DomainError {
49
+ readonly key?: string | undefined;
50
+ constructor(message: string, key?: string | undefined);
51
+ }
52
+ //#endregion
53
+ //#region src/external-service.errors.d.ts
54
+ declare class ExternalServiceError extends DomainError {
55
+ readonly service: string;
56
+ constructor(service: string, message: string, cause?: Error);
57
+ }
58
+ //#endregion
59
+ //#region src/duplicate-entity.errors.d.ts
60
+ declare class DuplicateEntityError extends DomainError {
61
+ readonly entityType: string;
62
+ readonly entityId: string;
63
+ constructor(entityType: string, entityId: string);
64
+ }
65
+ //#endregion
66
+ //#region src/authentication.errors.d.ts
67
+ declare class AuthenticationError extends DomainError {
68
+ constructor(message: string);
69
+ }
70
+ //#endregion
71
+ //#region src/not-found.errors.d.ts
72
+ declare class NotFoundError extends DomainError {
73
+ constructor(message: string);
74
+ }
75
+ //#endregion
76
+ //#region src/factories.d.ts
77
+ declare function createValidationError(message: string, field?: string, value?: unknown): ValidationError;
78
+ declare function createEntityNotFoundError(entityType: string, entityId: string): EntityNotFoundError;
79
+ declare function createRepositoryError(message: string, cause?: Error): RepositoryError;
80
+ declare function createPermissionDeniedError(resource: string, action: string): PermissionDeniedError;
81
+ declare function createDuplicateEntityError(entityType: string, entityId: string): DuplicateEntityError;
82
+ declare function createAuthenticationError(message: string): AuthenticationError;
83
+ declare function createNotFoundError(message: string): NotFoundError;
84
+ //#endregion
85
+ export { AuthenticationError, BusinessRuleError, ConcurrencyError, ConfigurationError, DomainError, DuplicateEntityError, EntityNotFoundError, ExternalServiceError, NotFoundError, PermissionDeniedError, RepositoryError, ValidationError, createAuthenticationError, createDuplicateEntityError, createEntityNotFoundError, createNotFoundError, createPermissionDeniedError, createRepositoryError, createValidationError };
@@ -0,0 +1,85 @@
1
+ //#region src/domain.errors.d.ts
2
+ declare abstract class DomainError extends Error {
3
+ readonly code: string;
4
+ readonly context?: Record<string, unknown> | undefined;
5
+ constructor(message: string, code?: string, context?: Record<string, unknown> | undefined);
6
+ toJSON(): object;
7
+ }
8
+ //#endregion
9
+ //#region src/validation.errors.d.ts
10
+ declare class ValidationError extends DomainError {
11
+ readonly field?: string | undefined;
12
+ readonly value?: unknown | undefined;
13
+ constructor(message: string, field?: string | undefined, value?: unknown | undefined);
14
+ }
15
+ //#endregion
16
+ //#region src/entity-not-found.errors.d.ts
17
+ declare class EntityNotFoundError extends DomainError {
18
+ readonly entityType: string;
19
+ readonly entityId: string;
20
+ constructor(entityType: string, entityId: string);
21
+ }
22
+ //#endregion
23
+ //#region src/repository.errors.d.ts
24
+ declare class RepositoryError extends DomainError {
25
+ readonly cause?: Error | undefined;
26
+ constructor(message: string, cause?: Error | undefined, context?: Record<string, unknown>);
27
+ toJSON(): object;
28
+ }
29
+ //#endregion
30
+ //#region src/permission-denied.errors.d.ts
31
+ declare class PermissionDeniedError extends DomainError {
32
+ readonly resource: string;
33
+ readonly action: string;
34
+ constructor(resource: string, action: string);
35
+ }
36
+ //#endregion
37
+ //#region src/business-rule.errors.d.ts
38
+ declare class BusinessRuleError extends DomainError {
39
+ constructor(message: string, rule?: string);
40
+ }
41
+ //#endregion
42
+ //#region src/concurrency.errors.d.ts
43
+ declare class ConcurrencyError extends DomainError {
44
+ constructor(message?: string);
45
+ }
46
+ //#endregion
47
+ //#region src/configuration.errors.d.ts
48
+ declare class ConfigurationError extends DomainError {
49
+ readonly key?: string | undefined;
50
+ constructor(message: string, key?: string | undefined);
51
+ }
52
+ //#endregion
53
+ //#region src/external-service.errors.d.ts
54
+ declare class ExternalServiceError extends DomainError {
55
+ readonly service: string;
56
+ constructor(service: string, message: string, cause?: Error);
57
+ }
58
+ //#endregion
59
+ //#region src/duplicate-entity.errors.d.ts
60
+ declare class DuplicateEntityError extends DomainError {
61
+ readonly entityType: string;
62
+ readonly entityId: string;
63
+ constructor(entityType: string, entityId: string);
64
+ }
65
+ //#endregion
66
+ //#region src/authentication.errors.d.ts
67
+ declare class AuthenticationError extends DomainError {
68
+ constructor(message: string);
69
+ }
70
+ //#endregion
71
+ //#region src/not-found.errors.d.ts
72
+ declare class NotFoundError extends DomainError {
73
+ constructor(message: string);
74
+ }
75
+ //#endregion
76
+ //#region src/factories.d.ts
77
+ declare function createValidationError(message: string, field?: string, value?: unknown): ValidationError;
78
+ declare function createEntityNotFoundError(entityType: string, entityId: string): EntityNotFoundError;
79
+ declare function createRepositoryError(message: string, cause?: Error): RepositoryError;
80
+ declare function createPermissionDeniedError(resource: string, action: string): PermissionDeniedError;
81
+ declare function createDuplicateEntityError(entityType: string, entityId: string): DuplicateEntityError;
82
+ declare function createAuthenticationError(message: string): AuthenticationError;
83
+ declare function createNotFoundError(message: string): NotFoundError;
84
+ //#endregion
85
+ export { AuthenticationError, BusinessRuleError, ConcurrencyError, ConfigurationError, DomainError, DuplicateEntityError, EntityNotFoundError, ExternalServiceError, NotFoundError, PermissionDeniedError, RepositoryError, ValidationError, createAuthenticationError, createDuplicateEntityError, createEntityNotFoundError, createNotFoundError, createPermissionDeniedError, createRepositoryError, createValidationError };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var e=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},t=class extends e{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},n=class extends e{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},r=class extends e{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},i=class extends e{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},a=class extends e{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},o=class extends e{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},s=class extends e{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},c=class extends e{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}},l=class extends e{constructor(e,t){super(`${e} 已存在: ${t}`,`DUPLICATE_ENTITY`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`DuplicateEntityError`}},u=class extends e{constructor(e){super(e,`AUTHENTICATION_ERROR`),this.name=`AuthenticationError`}},d=class extends e{constructor(e){super(e,`NOT_FOUND`),this.name=`NotFoundError`}};function f(e,n,r){return new t(e,n,r)}function p(e,t){return new n(e,t)}function m(e,t){return new r(e,t)}function h(e,t){return new i(e,t)}function g(e,t){return new l(e,t)}function _(e){return new u(e)}function v(e){return new d(e)}export{u as AuthenticationError,a as BusinessRuleError,o as ConcurrencyError,s as ConfigurationError,e as DomainError,l as DuplicateEntityError,n as EntityNotFoundError,c as ExternalServiceError,d as NotFoundError,i as PermissionDeniedError,r as RepositoryError,t as ValidationError,_ as createAuthenticationError,g as createDuplicateEntityError,p as createEntityNotFoundError,v as createNotFoundError,h as createPermissionDeniedError,m as createRepositoryError,f as createValidationError};
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@longzai-intelligence/error-core",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "types": "./dist/index.d.mts",
7
+ "exports": {
8
+ ".": {
9
+ "types": {
10
+ "import": "./dist/index.d.mts",
11
+ "require": "./dist/index.d.cts"
12
+ },
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsdown",
22
+ "build:prod": "NODE_ENV=production tsdown",
23
+ "prepublishOnly": "bun run build:prod",
24
+ "dev": "tsdown --watch",
25
+ "clean": "rimraf dist tsconfig/.cache",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest",
28
+ "test:coverage": "vitest run --coverage",
29
+ "lint": "eslint .",
30
+ "lint:fix": "eslint . --fix",
31
+ "typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
32
+ "typecheck:app": "tsc --noEmit -p tsconfig/app.json",
33
+ "typecheck:node": "tsc --noEmit -p tsconfig/node.json",
34
+ "typecheck:test": "tsc --noEmit -p tsconfig/test.json",
35
+ "upgrade-deps": "ncu -u"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bun": "1.3.11",
39
+ "@types/node": "^25.5.2",
40
+ "eslint": "^10.2.0",
41
+ "rimraf": "^6.1.3",
42
+ "typescript": "^6.0.2",
43
+ "vitest": "^4.1.3",
44
+ "@longzai-intelligence/eslint-preset-library": "0.0.5",
45
+ "@longzai-intelligence/vitest-config": "0.0.4",
46
+ "@longzai-intelligence/typescript-config": "0.0.2",
47
+ "tsdown": "^0.21.7",
48
+ "@longzai-intelligence/tsdown-config": "0.0.1"
49
+ },
50
+ "keywords": [
51
+ "error",
52
+ "core",
53
+ "domain",
54
+ "typescript"
55
+ ],
56
+ "license": "UNLICENSED",
57
+ "module": "./dist/index.mjs"
58
+ }