ec-nest/src/common/error-codes.ts
liuyun 8eefa7fa44 feat(error): 业务错误码注册表(业务域+错误序)+ BusinessException
- error-codes 单一注册表 + BusinessException + 过滤器识别
- 迁移登录/锁定/待改密/用户名冲突/租户/会话等关键错误

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 22:42:12 +08:00

34 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 业务错误码单一事实源:5 位数字 [域码 2 位][序 3 位]。
*
* 域码:00 通用 · 10 认证 · 11 用户 · 12 角色 · 13 资源 · 14 组织 · 15 租户
* 16 字典 · 17 配置 · 18 审计 · 19 登录 · 20 会话 · 21 通知。
*
* 用法:先在此登记,再 `throw new BusinessException(ErrorCodes.XXX, '...', HttpStatus.XXX)`。
* 未使用业务码的异常由 AllExceptionsFilter 回退为 `HTTP 状态 × 100`。
*/
export const ErrorCodes = {
// 认证(10)
AUTH_CREDENTIALS: 10001, // 账号或密码错误(401)
AUTH_LOCKED: 10002, // 账号已锁定(401)
AUTH_DISABLED: 10003, // 账号已停用(401)
AUTH_MUST_CHANGE_PWD: 10004, // 需先修改初始密码(403)
AUTH_OLD_PWD_WRONG: 10005, // 原密码错误(400)
AUTH_TOKEN_INVALID: 10006, // token 无效或已过期(401)
AUTH_SESSION_REVOKED: 10007, // 会话已失效(401)
// 用户(11)
USER_NOT_FOUND: 11001, // 用户不存在(404)
USER_USERNAME_EXISTS: 11002, // 用户名已存在(409)
// 租户(15)
TENANT_NOT_FOUND: 15001, // 租户不存在(404)
TENANT_EXISTS: 15002, // 租户已存在(409)
TENANT_DISABLED: 15003, // 租户不存在或已停用(401)
// 会话(20)
SESSION_NOT_FOUND: 20001, // 会话不存在或已下线(404)
} as const;
export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];