@buka/nestjs-kit 4.0.0 → 4.1.2
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/CHANGELOG.md +22 -3
- package/dist/index.d.mts +75 -1
- package/dist/index.d.ts +75 -1
- package/dist/index.js +187 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +170 -19
- package/dist/index.mjs.map +1 -1
- package/docs/core/urn.md +11 -11
- package/docs/exception.md +123 -121
- package/docs/index.md +6 -0
- package/docs/logger.md +289 -0
- package/package.json +6 -4
package/docs/core/urn.md
CHANGED
|
@@ -16,12 +16,12 @@ import {
|
|
|
16
16
|
## URN 格式
|
|
17
17
|
|
|
18
18
|
```
|
|
19
|
-
urn:buka
|
|
19
|
+
urn:buka:<namespace>[:<resource>[:<identifier>]]
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
| 层级 | 含义 | 规则 |
|
|
23
23
|
| ------------ | ----------------------------- | ------------------------------------------------ |
|
|
24
|
-
| `buka
|
|
24
|
+
| `buka` | 组织前缀 | 固定值 |
|
|
25
25
|
| `namespace` | 资源类型所有者(概念域/平台) | 小写 kebab-case,如 `galaxy`、`infra` |
|
|
26
26
|
| `resource` | 资源类型 | 小写 kebab-case,如 `principal`、`oauth2-client` |
|
|
27
27
|
| `identifier` | 资源唯一标识 | UUID、hex 或其他自定义格式 |
|
|
@@ -30,11 +30,11 @@ urn:buka-inc:<namespace>[:<resource>[:<identifier>]]
|
|
|
30
30
|
|
|
31
31
|
URN 支持三种粒度,`resource` 和 `identifier` 均为可选:
|
|
32
32
|
|
|
33
|
-
| 粒度 | 类型 | 格式
|
|
34
|
-
| ------------ | --------------- |
|
|
35
|
-
| namespace 级 | `NamespaceUrn` | `urn:buka
|
|
36
|
-
| 资源类型级 | `ResourceUrn` | `urn:buka
|
|
37
|
-
| 实例级 | `IdentifierUrn` | `urn:buka
|
|
33
|
+
| 粒度 | 类型 | 格式 | 示例 | 用途 |
|
|
34
|
+
| ------------ | --------------- | ------------------------------- | ---------------------------------------- | ---------------------------------- |
|
|
35
|
+
| namespace 级 | `NamespaceUrn` | `urn:buka:<ns>` | `urn:buka:galaxy` | 标识平台/系统整体,如 JWT audience |
|
|
36
|
+
| 资源类型级 | `ResourceUrn` | `urn:buka:<ns>:<resource>` | `urn:buka:galaxy:principal` | 标识一类资源 |
|
|
37
|
+
| 实例级 | `IdentifierUrn` | `urn:buka:<ns>:<resource>:<id>` | `urn:buka:galaxy:principal:550e8400-...` | 标识具体资源实例,如 JWT subject |
|
|
38
38
|
|
|
39
39
|
### namespace 划分原则
|
|
40
40
|
|
|
@@ -69,12 +69,12 @@ static parse(urn: string): Urn
|
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
```typescript
|
|
72
|
-
const urn = Urn.parse("urn:buka
|
|
72
|
+
const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
|
|
73
73
|
urn.namespace; // → 'galaxy'
|
|
74
74
|
urn.resource; // → 'principal'
|
|
75
75
|
urn.identifier; // → 'abc-123'
|
|
76
76
|
|
|
77
|
-
const nsUrn = Urn.parse("urn:buka
|
|
77
|
+
const nsUrn = Urn.parse("urn:buka:galaxy");
|
|
78
78
|
nsUrn.namespace; // → 'galaxy'
|
|
79
79
|
nsUrn.resource; // → undefined
|
|
80
80
|
nsUrn.identifier; // → undefined
|
|
@@ -92,7 +92,7 @@ toString(): string
|
|
|
92
92
|
|
|
93
93
|
```typescript
|
|
94
94
|
Urn.of("galaxy", "principal", "abc-123").toString();
|
|
95
|
-
// → 'urn:buka
|
|
95
|
+
// → 'urn:buka:galaxy:principal:abc-123'
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
### is
|
|
@@ -104,7 +104,7 @@ is(namespace: string, resource?: string, identifier?: string): boolean
|
|
|
104
104
|
```
|
|
105
105
|
|
|
106
106
|
```typescript
|
|
107
|
-
const urn = Urn.parse("urn:buka
|
|
107
|
+
const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
|
|
108
108
|
|
|
109
109
|
urn.is("galaxy"); // → true(仅匹配 namespace)
|
|
110
110
|
urn.is("galaxy", "principal"); // → true
|
package/docs/exception.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## 概述
|
|
4
4
|
|
|
5
|
-
`ExceptionModule` 提供统一的异常处理机制,基于结构化错误码体系。错误码规范详见 [`@buka/error-codes` 规范文档](https://github.com/buka-
|
|
5
|
+
`ExceptionModule` 提供统一的异常处理机制,基于结构化错误码体系。错误码规范详见 [`@buka/error-codes` 规范文档](https://github.com/buka-ltd/npm.error-codes/blob/main/docs/specification.md)。
|
|
6
6
|
|
|
7
7
|
模块核心能力:
|
|
8
8
|
|
|
@@ -18,22 +18,22 @@ import {
|
|
|
18
18
|
BusinessException,
|
|
19
19
|
SystemException,
|
|
20
20
|
ErrorCodeRegistry,
|
|
21
|
-
} from
|
|
21
|
+
} from "@buka/nestjs-kit";
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
## 模块配置
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
27
|
// 使用十进制数字
|
|
28
|
-
ExceptionModule.register({ systemId: 1001 })
|
|
28
|
+
ExceptionModule.register({ systemId: 1001 });
|
|
29
29
|
|
|
30
30
|
// 使用 Crockford Base32 字符串(等价于上面的 1001)
|
|
31
|
-
ExceptionModule.register({ systemId:
|
|
31
|
+
ExceptionModule.register({ systemId: "Z9" });
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
| 选项
|
|
35
|
-
|
|
36
|
-
| `systemId` | `number \| string` | 是
|
|
34
|
+
| 选项 | 类型 | 必填 | 说明 |
|
|
35
|
+
| ---------- | ------------------ | ---- | -------------------------------------------------------------------------------------------------- |
|
|
36
|
+
| `systemId` | `number \| string` | 是 | 系统 ID(0 - 1048575),全公司唯一,用于区分不同业务系统。支持十进制数字或 Crockford Base32 字符串 |
|
|
37
37
|
|
|
38
38
|
`register` 内部会将 `systemId` 注册到 `ErrorCodeRegistry`,并自动添加 `ErrorCodeExceptionFilter` 为全局异常过滤器。
|
|
39
39
|
|
|
@@ -41,17 +41,17 @@ ExceptionModule.register({ systemId: 'Z9' })
|
|
|
41
41
|
|
|
42
42
|
提供 9 种异常工厂,每种对应一个 `ErrorCategory` 和默认 HTTP 状态码:
|
|
43
43
|
|
|
44
|
-
| 工厂函数
|
|
45
|
-
|
|
46
|
-
| `SystemException`
|
|
47
|
-
| `BusinessException`
|
|
48
|
-
| `ValidationException` | `VALIDATION`
|
|
49
|
-
| `ThirdPartyException` | `THIRD_PARTY` | 502
|
|
50
|
-
| `AuthException`
|
|
51
|
-
| `RateLimitException`
|
|
52
|
-
| `DegradeException`
|
|
53
|
-
| `ConflictException`
|
|
54
|
-
| `FeatureException`
|
|
44
|
+
| 工厂函数 | ErrorCategory | 默认 HTTP 状态码 | 适用场景 |
|
|
45
|
+
| --------------------- | ------------- | ---------------- | ------------------------------------ |
|
|
46
|
+
| `SystemException` | `SYSTEM` | 500 | 系统级错误(数据库故障、内部错误) |
|
|
47
|
+
| `BusinessException` | `BUSINESS` | 400 | 业务逻辑错误(资源不存在、余额不足) |
|
|
48
|
+
| `ValidationException` | `VALIDATION` | 400 | 参数校验错误 |
|
|
49
|
+
| `ThirdPartyException` | `THIRD_PARTY` | 502 | 第三方服务调用失败 |
|
|
50
|
+
| `AuthException` | `AUTH` | 401 | 认证/授权错误 |
|
|
51
|
+
| `RateLimitException` | `RATE_LIMIT` | 429 | 请求被限流 |
|
|
52
|
+
| `DegradeException` | `DEGRADE` | 503 | 服务降级 |
|
|
53
|
+
| `ConflictException` | `CONFLICT` | 409 | 资源状态冲突 |
|
|
54
|
+
| `FeatureException` | `FEATURE` | 403 | 功能未开放或权限不足 |
|
|
55
55
|
|
|
56
56
|
### 固定消息模式
|
|
57
57
|
|
|
@@ -68,8 +68,8 @@ static readonly NotFound = BusinessException({
|
|
|
68
68
|
生成的异常类支持两种构造方式:
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
|
-
throw new UserExceptions.NotFound()
|
|
72
|
-
throw new UserExceptions.NotFound(
|
|
71
|
+
throw new UserExceptions.NotFound(); // 使用默认消息
|
|
72
|
+
throw new UserExceptions.NotFound("自定义消息"); // 覆盖默认消息
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
### 消息工厂模式
|
|
@@ -83,19 +83,19 @@ static readonly NotFoundById = BusinessException({
|
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
```typescript
|
|
86
|
-
throw new UserExceptions.NotFoundById(
|
|
86
|
+
throw new UserExceptions.NotFoundById("123");
|
|
87
87
|
// → 消息: "User 123 not found"
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
### 工厂选项
|
|
91
91
|
|
|
92
|
-
| 选项
|
|
93
|
-
|
|
94
|
-
| `sequenceId`
|
|
95
|
-
| `message`
|
|
96
|
-
| `messageFactory` | `(...args) => string` | 与 `message` 二选一
|
|
97
|
-
| `httpStatus`
|
|
98
|
-
| `description`
|
|
92
|
+
| 选项 | 类型 | 必填 | 说明 |
|
|
93
|
+
| ---------------- | --------------------- | -------------------------- | ----------------------------------------------------------------------------------------- |
|
|
94
|
+
| `sequenceId` | `number` | 是 | 序列号(0 - 32767),同模块内同类别唯一 |
|
|
95
|
+
| `message` | `string` | 与 `messageFactory` 二选一 | 固定错误消息 |
|
|
96
|
+
| `messageFactory` | `(...args) => string` | 与 `message` 二选一 | 消息工厂函数 |
|
|
97
|
+
| `httpStatus` | `HttpStatus` | 否 | 自定义 HTTP 状态码,默认使用类别对应的状态码 |
|
|
98
|
+
| `description` | `string` | 否 | 错误描述,用于错误码查询接口展示详细信息和解决方案。固定消息模式下默认使用 `message` 的值 |
|
|
99
99
|
|
|
100
100
|
## @ModuleExceptions 装饰器
|
|
101
101
|
|
|
@@ -103,32 +103,34 @@ throw new UserExceptions.NotFoundById('123')
|
|
|
103
103
|
|
|
104
104
|
```typescript
|
|
105
105
|
// 使用 Crockford Base32 字符串(推荐,与错误码可读格式一致)
|
|
106
|
-
@ModuleExceptions({ moduleId:
|
|
106
|
+
@ModuleExceptions({ moduleId: "1000" })
|
|
107
107
|
export class UserExceptions {
|
|
108
108
|
static readonly NotFound = BusinessException({
|
|
109
109
|
sequenceId: 1,
|
|
110
|
-
message:
|
|
111
|
-
})
|
|
110
|
+
message: "User not found",
|
|
111
|
+
});
|
|
112
112
|
|
|
113
113
|
static readonly AlreadyExists = BusinessException({
|
|
114
114
|
sequenceId: 2,
|
|
115
|
-
message:
|
|
116
|
-
})
|
|
115
|
+
message: "User already exists",
|
|
116
|
+
});
|
|
117
117
|
|
|
118
118
|
static readonly InvalidEmail = ValidationException({
|
|
119
119
|
sequenceId: 1,
|
|
120
|
-
message:
|
|
121
|
-
})
|
|
120
|
+
message: "Invalid email format",
|
|
121
|
+
});
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
// 也支持十进制数字(等价于 '1000',即 32768)
|
|
125
125
|
@ModuleExceptions({ moduleId: 32768 })
|
|
126
|
-
export class UserExceptions {
|
|
126
|
+
export class UserExceptions {
|
|
127
|
+
/* ... */
|
|
128
|
+
}
|
|
127
129
|
```
|
|
128
130
|
|
|
129
|
-
| 选项
|
|
130
|
-
|
|
131
|
-
| `moduleId` | `number \| string` | 是
|
|
131
|
+
| 选项 | 类型 | 必填 | 说明 |
|
|
132
|
+
| ---------- | ------------------ | ---- | ------------------------------------------------------------------------------------------------- |
|
|
133
|
+
| `moduleId` | `number \| string` | 是 | 模块 ID,每个模块唯一。支持十进制数字(0 - 1048575)或 Crockford Base32 字符串(`0000` - `ZZZZ`) |
|
|
132
134
|
|
|
133
135
|
> `moduleId: 0` 为系统保留,用于映射 NestJS 内置 HTTP 异常。业务模块应在 `1000` - `FZZZ` 范围内分配。
|
|
134
136
|
|
|
@@ -138,16 +140,16 @@ export class UserExceptions { /* ... */ }
|
|
|
138
140
|
|
|
139
141
|
### 常用方法
|
|
140
142
|
|
|
141
|
-
| 方法
|
|
142
|
-
|
|
143
|
-
| `getInstance()`
|
|
144
|
-
| `setSystemId(id)`
|
|
145
|
-
| `getSystemId()`
|
|
146
|
-
| `registerModule(moduleId, name)`
|
|
143
|
+
| 方法 | 说明 |
|
|
144
|
+
| ----------------------------------------- | -------------------------------------------- |
|
|
145
|
+
| `getInstance()` | 获取单例实例 |
|
|
146
|
+
| `setSystemId(id)` | 配置系统 ID(仅可设置一次) |
|
|
147
|
+
| `getSystemId()` | 获取系统 ID |
|
|
148
|
+
| `registerModule(moduleId, name)` | 注册模块 ID |
|
|
147
149
|
| `register(meta, className, description?)` | 注册错误码(自动校验唯一性),可附带描述信息 |
|
|
148
|
-
| `getDescription(key)`
|
|
149
|
-
| `getAllRegisteredCodes()`
|
|
150
|
-
| `getAllRegisteredModules()`
|
|
150
|
+
| `getDescription(key)` | 获取错误码描述 |
|
|
151
|
+
| `getAllRegisteredCodes()` | 获取所有已注册的错误码 |
|
|
152
|
+
| `getAllRegisteredModules()` | 获取所有已注册的模块 |
|
|
151
153
|
|
|
152
154
|
> 通常不需要直接调用 `ErrorCodeRegistry`,`ExceptionModule.register()` 和 `@ModuleExceptions` 会自动完成注册。
|
|
153
155
|
|
|
@@ -155,10 +157,10 @@ export class UserExceptions { /* ... */ }
|
|
|
155
157
|
|
|
156
158
|
`ExceptionModule` 自动注册 `GET /error-codes` 接口,返回所有已注册错误码的列表。每个错误码条目包含 `metadata` 字段:
|
|
157
159
|
|
|
158
|
-
| 字段
|
|
159
|
-
|
|
160
|
-
| `moduleName`
|
|
161
|
-
| `phrase`
|
|
160
|
+
| 字段 | 类型 | 说明 |
|
|
161
|
+
| ------------- | --------- | -------------------------------- |
|
|
162
|
+
| `moduleName` | `string` | 模块名称 |
|
|
163
|
+
| `phrase` | `string` | 错误短语(异常类的属性名) |
|
|
162
164
|
| `description` | `string?` | 错误描述,包含详细信息和解决方案 |
|
|
163
165
|
|
|
164
166
|
`description` 来源于异常工厂函数的 `description` 选项。固定消息模式下若未指定,默认使用 `message` 值;消息工厂模式下若未指定则为空。
|
|
@@ -185,29 +187,29 @@ export class UserExceptions { /* ... */ }
|
|
|
185
187
|
}
|
|
186
188
|
```
|
|
187
189
|
|
|
188
|
-
| 字段
|
|
189
|
-
|
|
190
|
-
| `code`
|
|
191
|
-
| `message` | `string`
|
|
192
|
-
| `details` | `ExceptionDetail[]` | 异常详情数组
|
|
190
|
+
| 字段 | 类型 | 说明 |
|
|
191
|
+
| --------- | ------------------- | ------------------------------------------------------------------ |
|
|
192
|
+
| `code` | `string` | 结构化错误码字符串,格式由 `@buka/error-codes` 的 `ErrorCode` 定义 |
|
|
193
|
+
| `message` | `string` | 错误消息 |
|
|
194
|
+
| `details` | `ExceptionDetail[]` | 异常详情数组 |
|
|
193
195
|
|
|
194
196
|
### HTTP 状态码映射
|
|
195
197
|
|
|
196
198
|
NestJS 内置 HTTP 异常会被映射到预留错误码(`moduleId: 0`):
|
|
197
199
|
|
|
198
200
|
| HTTP 状态码 | ErrorCategory | 保留 sequenceId |
|
|
199
|
-
|
|
200
|
-
| 400
|
|
201
|
-
| 401
|
|
202
|
-
| 403
|
|
203
|
-
| 404
|
|
204
|
-
| 409
|
|
205
|
-
| 415
|
|
206
|
-
| 429
|
|
207
|
-
| 500
|
|
208
|
-
| 502
|
|
209
|
-
| 503
|
|
210
|
-
| 504
|
|
201
|
+
| ----------- | ------------- | --------------- |
|
|
202
|
+
| 400 | `VALIDATION` | 400 |
|
|
203
|
+
| 401 | `AUTH` | 401 |
|
|
204
|
+
| 403 | `AUTH` | 403 |
|
|
205
|
+
| 404 | `BUSINESS` | 404 |
|
|
206
|
+
| 409 | `CONFLICT` | 409 |
|
|
207
|
+
| 415 | `VALIDATION` | 415 |
|
|
208
|
+
| 429 | `RATE_LIMIT` | 429 |
|
|
209
|
+
| 500 | `SYSTEM` | 500 |
|
|
210
|
+
| 502 | `THIRD_PARTY` | 502 |
|
|
211
|
+
| 503 | `DEGRADE` | 503 |
|
|
212
|
+
| 504 | `THIRD_PARTY` | 504 |
|
|
211
213
|
|
|
212
214
|
> `moduleId: 0` 为系统保留,业务模块应从 1 开始分配。
|
|
213
215
|
|
|
@@ -216,30 +218,30 @@ NestJS 内置 HTTP 异常会被映射到预留错误码(`moduleId: 0`):
|
|
|
216
218
|
`BukaExceptions` 是 `moduleId: 0` 对应的内置异常类,与上表中的 HTTP 状态码映射一一对应。可以直接使用这些异常,而不必依赖 NestJS 内置 `HttpException` 被 filter 隐式映射:
|
|
217
219
|
|
|
218
220
|
```typescript
|
|
219
|
-
import { BukaExceptions } from
|
|
221
|
+
import { BukaExceptions } from "@buka/nestjs-kit";
|
|
220
222
|
|
|
221
223
|
// 直接抛出内置异常
|
|
222
|
-
throw new BukaExceptions.BadRequest()
|
|
223
|
-
throw new BukaExceptions.BadRequest(
|
|
224
|
-
throw new BukaExceptions.NotFound()
|
|
225
|
-
throw new BukaExceptions.Unauthorized()
|
|
224
|
+
throw new BukaExceptions.BadRequest();
|
|
225
|
+
throw new BukaExceptions.BadRequest("自定义消息");
|
|
226
|
+
throw new BukaExceptions.NotFound();
|
|
227
|
+
throw new BukaExceptions.Unauthorized();
|
|
226
228
|
```
|
|
227
229
|
|
|
228
230
|
完整列表:
|
|
229
231
|
|
|
230
|
-
| 异常
|
|
231
|
-
|
|
232
|
-
| `BukaExceptions.BadRequest`
|
|
233
|
-
| `BukaExceptions.UnsupportedMediaType` | `VALIDATION`
|
|
234
|
-
| `BukaExceptions.Unauthorized`
|
|
235
|
-
| `BukaExceptions.Forbidden`
|
|
236
|
-
| `BukaExceptions.NotFound`
|
|
237
|
-
| `BukaExceptions.Conflict`
|
|
238
|
-
| `BukaExceptions.TooManyRequests`
|
|
239
|
-
| `BukaExceptions.InternalServerError`
|
|
240
|
-
| `BukaExceptions.BadGateway`
|
|
241
|
-
| `BukaExceptions.GatewayTimeout`
|
|
242
|
-
| `BukaExceptions.ServiceUnavailable`
|
|
232
|
+
| 异常 | ErrorCategory | sequenceId |
|
|
233
|
+
| ------------------------------------- | ------------- | ---------- |
|
|
234
|
+
| `BukaExceptions.BadRequest` | `VALIDATION` | 400 |
|
|
235
|
+
| `BukaExceptions.UnsupportedMediaType` | `VALIDATION` | 415 |
|
|
236
|
+
| `BukaExceptions.Unauthorized` | `AUTH` | 401 |
|
|
237
|
+
| `BukaExceptions.Forbidden` | `AUTH` | 403 |
|
|
238
|
+
| `BukaExceptions.NotFound` | `BUSINESS` | 404 |
|
|
239
|
+
| `BukaExceptions.Conflict` | `CONFLICT` | 409 |
|
|
240
|
+
| `BukaExceptions.TooManyRequests` | `RATE_LIMIT` | 429 |
|
|
241
|
+
| `BukaExceptions.InternalServerError` | `SYSTEM` | 500 |
|
|
242
|
+
| `BukaExceptions.BadGateway` | `THIRD_PARTY` | 502 |
|
|
243
|
+
| `BukaExceptions.GatewayTimeout` | `THIRD_PARTY` | 504 |
|
|
244
|
+
| `BukaExceptions.ServiceUnavailable` | `DEGRADE` | 503 |
|
|
243
245
|
|
|
244
246
|
如需禁用全局过滤器以自定义异常处理:
|
|
245
247
|
|
|
@@ -247,7 +249,7 @@ throw new BukaExceptions.Unauthorized()
|
|
|
247
249
|
ExceptionModule.register({
|
|
248
250
|
systemId: 1001,
|
|
249
251
|
useGlobalFilter: false,
|
|
250
|
-
})
|
|
252
|
+
});
|
|
251
253
|
```
|
|
252
254
|
|
|
253
255
|
## 异常详情
|
|
@@ -255,10 +257,10 @@ ExceptionModule.register({
|
|
|
255
257
|
通过实现 `ExceptionDetail` 接口来定义结构化的错误详情:
|
|
256
258
|
|
|
257
259
|
```typescript
|
|
258
|
-
import { ExceptionDetail, ValidationException } from
|
|
260
|
+
import { ExceptionDetail, ValidationException } from "@buka/nestjs-kit";
|
|
259
261
|
|
|
260
262
|
class FieldValidationDetail implements ExceptionDetail {
|
|
261
|
-
readonly type =
|
|
263
|
+
readonly type = "field_validation";
|
|
262
264
|
constructor(
|
|
263
265
|
public readonly field: string,
|
|
264
266
|
public readonly message: string,
|
|
@@ -269,14 +271,14 @@ class FieldValidationDetail implements ExceptionDetail {
|
|
|
269
271
|
export class InvalidUserInputException extends ValidationException({
|
|
270
272
|
moduleId: 10,
|
|
271
273
|
sequenceId: 10,
|
|
272
|
-
message:
|
|
274
|
+
message: "Invalid user input",
|
|
273
275
|
}) {}
|
|
274
276
|
|
|
275
277
|
// 使用
|
|
276
|
-
throw new InvalidUserInputException(
|
|
277
|
-
new FieldValidationDetail(
|
|
278
|
-
new FieldValidationDetail(
|
|
279
|
-
])
|
|
278
|
+
throw new InvalidUserInputException("Validation failed", [
|
|
279
|
+
new FieldValidationDetail("email", "Invalid email format"),
|
|
280
|
+
new FieldValidationDetail("age", "Must be a positive number"),
|
|
281
|
+
]);
|
|
280
282
|
```
|
|
281
283
|
|
|
282
284
|
## 完整使用示例
|
|
@@ -284,13 +286,13 @@ throw new InvalidUserInputException('Validation failed', [
|
|
|
284
286
|
### 1. 配置模块
|
|
285
287
|
|
|
286
288
|
```typescript
|
|
287
|
-
import { Module } from
|
|
288
|
-
import { ExceptionModule } from
|
|
289
|
+
import { Module } from "@nestjs/common";
|
|
290
|
+
import { ExceptionModule } from "@buka/nestjs-kit";
|
|
289
291
|
|
|
290
292
|
@Module({
|
|
291
293
|
imports: [
|
|
292
294
|
// systemId 支持十进制数字或 Crockford Base32 字符串
|
|
293
|
-
ExceptionModule.register({ systemId:
|
|
295
|
+
ExceptionModule.register({ systemId: "Z9" }),
|
|
294
296
|
],
|
|
295
297
|
})
|
|
296
298
|
export class AppModule {}
|
|
@@ -304,52 +306,52 @@ import {
|
|
|
304
306
|
BusinessException,
|
|
305
307
|
ValidationException,
|
|
306
308
|
AuthException,
|
|
307
|
-
} from
|
|
309
|
+
} from "@buka/nestjs-kit";
|
|
308
310
|
|
|
309
311
|
// moduleId 支持 Crockford Base32 字符串(推荐)或十进制数字
|
|
310
|
-
@ModuleExceptions({ moduleId:
|
|
312
|
+
@ModuleExceptions({ moduleId: "1000" })
|
|
311
313
|
export class UserExceptions {
|
|
312
314
|
static readonly NotFound = BusinessException({
|
|
313
315
|
sequenceId: 1,
|
|
314
|
-
message:
|
|
315
|
-
description:
|
|
316
|
-
})
|
|
316
|
+
message: "User not found",
|
|
317
|
+
description: "根据条件未找到用户,请确认用户是否存在",
|
|
318
|
+
});
|
|
317
319
|
|
|
318
320
|
static readonly NotFoundById = BusinessException({
|
|
319
321
|
sequenceId: 2,
|
|
320
322
|
messageFactory: (userId: string) => `User ${userId} not found`,
|
|
321
|
-
description:
|
|
322
|
-
})
|
|
323
|
+
description: "根据 ID 查询用户不存在,请检查 ID 是否正确",
|
|
324
|
+
});
|
|
323
325
|
|
|
324
326
|
static readonly InvalidEmail = ValidationException({
|
|
325
327
|
sequenceId: 1,
|
|
326
|
-
message:
|
|
327
|
-
})
|
|
328
|
+
message: "Invalid email format",
|
|
329
|
+
});
|
|
328
330
|
|
|
329
331
|
static readonly Unauthorized = AuthException({
|
|
330
332
|
sequenceId: 1,
|
|
331
|
-
message:
|
|
332
|
-
})
|
|
333
|
+
message: "Authentication required",
|
|
334
|
+
});
|
|
333
335
|
}
|
|
334
336
|
```
|
|
335
337
|
|
|
336
338
|
### 3. 在 Controller 中抛出异常
|
|
337
339
|
|
|
338
340
|
```typescript
|
|
339
|
-
import { Controller, Get, Param } from
|
|
340
|
-
import { UserExceptions } from
|
|
341
|
+
import { Controller, Get, Param } from "@nestjs/common";
|
|
342
|
+
import { UserExceptions } from "./user.exceptions";
|
|
341
343
|
|
|
342
|
-
@Controller(
|
|
344
|
+
@Controller("users")
|
|
343
345
|
export class UserController {
|
|
344
|
-
@Get(
|
|
345
|
-
async findOne(@Param(
|
|
346
|
-
const user = await this.userService.findOne(id)
|
|
346
|
+
@Get(":id")
|
|
347
|
+
async findOne(@Param("id") id: string) {
|
|
348
|
+
const user = await this.userService.findOne(id);
|
|
347
349
|
|
|
348
350
|
if (!user) {
|
|
349
|
-
throw new UserExceptions.NotFoundById(id)
|
|
351
|
+
throw new UserExceptions.NotFoundById(id);
|
|
350
352
|
}
|
|
351
353
|
|
|
352
|
-
return user
|
|
354
|
+
return user;
|
|
353
355
|
}
|
|
354
356
|
}
|
|
355
357
|
```
|
|
@@ -360,20 +362,20 @@ export class UserController {
|
|
|
360
362
|
|
|
361
363
|
```typescript
|
|
362
364
|
// packages/payment-sdk/src/payment.exceptions.ts
|
|
363
|
-
import { ThirdPartyException, BusinessException } from
|
|
365
|
+
import { ThirdPartyException, BusinessException } from "@buka/nestjs-kit";
|
|
364
366
|
|
|
365
|
-
const MODULE_ID = 100 // 支付模块
|
|
367
|
+
const MODULE_ID = 100; // 支付模块
|
|
366
368
|
|
|
367
369
|
export class PaymentTimeoutException extends ThirdPartyException({
|
|
368
370
|
moduleId: MODULE_ID,
|
|
369
371
|
sequenceId: 1,
|
|
370
|
-
message:
|
|
372
|
+
message: "Payment gateway timeout",
|
|
371
373
|
}) {}
|
|
372
374
|
|
|
373
375
|
export class PaymentGatewayErrorException extends ThirdPartyException({
|
|
374
376
|
moduleId: MODULE_ID,
|
|
375
377
|
sequenceId: 2,
|
|
376
|
-
message:
|
|
378
|
+
message: "Payment gateway error",
|
|
377
379
|
}) {}
|
|
378
380
|
```
|
|
379
381
|
|
package/docs/index.md
CHANGED
|
@@ -14,6 +14,7 @@ pnpm install @buka/nestjs-kit
|
|
|
14
14
|
import { Module } from "@nestjs/common";
|
|
15
15
|
import {
|
|
16
16
|
BukaModule,
|
|
17
|
+
LoggerModule,
|
|
17
18
|
ExceptionModule,
|
|
18
19
|
BlindIndexModule,
|
|
19
20
|
SaltedHashModule,
|
|
@@ -24,6 +25,9 @@ import {
|
|
|
24
25
|
// 核心模块(全局验证管道)
|
|
25
26
|
BukaModule.register({}),
|
|
26
27
|
|
|
28
|
+
// 统一日志(Grafana/Loki 友好 JSON 格式)
|
|
29
|
+
LoggerModule.register({ serviceName: "my-service" }),
|
|
30
|
+
|
|
27
31
|
// 异常处理(systemId 支持十进制数字或 Crockford Base32 字符串)
|
|
28
32
|
ExceptionModule.register({ systemId: "Z9" }),
|
|
29
33
|
|
|
@@ -47,6 +51,7 @@ export class AppModule {}
|
|
|
47
51
|
| `BlindIndexModule` | 对加密数据生成可搜索的哈希索引 | [blind-index.md](./blind-index.md) |
|
|
48
52
|
| `SaltedHashModule` | 密码安全存储与校验(bcrypt) | [salted-hash.md](./salted-hash.md) |
|
|
49
53
|
| `ObjectStorageModule` | S3 兼容的对象存储客户端 | [object-storage.md](./object-storage.md) |
|
|
54
|
+
| `LoggerModule` | 统一日志方案,基于 nestjs-pino,Grafana/Loki 友好格式 | [logger.md](./logger.md) |
|
|
50
55
|
| **SwaggerPatcher** | Swagger 文档后处理工具 | [swagger-patcher.md](./swagger-patcher.md) |
|
|
51
56
|
|
|
52
57
|
## 模块依赖关系
|
|
@@ -54,6 +59,7 @@ export class AppModule {}
|
|
|
54
59
|
```
|
|
55
60
|
BukaModule (独立,建议所有项目引入)
|
|
56
61
|
ExceptionModule (独立)
|
|
62
|
+
LoggerModule (独立)
|
|
57
63
|
OpenBaoModule (独立)
|
|
58
64
|
EnvelopeEncryptionModule ──依赖──▶ OpenBaoModule
|
|
59
65
|
BlindIndexModule (独立)
|