| 1 | --- |
| 2 | id: aitoearn-exception-handling |
| 3 | trigger: "when throwing exceptions or handling errors" |
| 4 | confidence: 0.95 |
| 5 | domain: typescript |
| 6 | source: local-repo-analysis |
| 7 | --- |
| 8 | |
| 9 | # AppException Pattern |
| 10 | |
| 11 | ## Action |
| 12 | Only use `AppException` with `ResponseCode` for business errors. |
| 13 | |
| 14 | ## Usage |
| 15 | ```typescript |
| 16 | import { AppException, ResponseCode } from '@yikart/common' |
| 17 | |
| 18 | // Only code |
| 19 | throw new AppException(ResponseCode.MaterialGroupNotFound) |
| 20 | |
| 21 | // Code + data |
| 22 | throw new AppException(ResponseCode.MaterialGroupNotFound, { groupId: 'group_xxx' }) |
| 23 | ``` |
| 24 | |
| 25 | ## ResponseCode Rules |
| 26 | - Success: `Success = 0` |
| 27 | - Business errors: Start from `10000`, allocated by module |
| 28 | - Naming: PascalCase, specific resource name |
| 29 | - Location: Only in `libs/common/src/enums/response-code.enum.ts` |
| 30 | |
| 31 | ## Adding New Error Code |
| 32 | 1. Add constant in `ResponseCode` enum |
| 33 | 2. Add message mapping in `libs/common/src/i18n/messages.ts` |
| 34 | 3. Use in business code |
| 35 | |
| 36 | ## Forbidden |
| 37 | - ❌ Custom message override |
| 38 | - ❌ Generic names: `Unauthorized`, `PermissionDenied`, `AccessDenied` |
| 39 | - ❌ Custom exception classes |
| 40 | - ❌ Custom HTTP status codes in business code |
| 41 | |
| 42 | ## Correct Naming |
| 43 | - ✅ `ContractNotFound` |
| 44 | - ✅ `CommentNotFound` |
| 45 | - ✅ `MaterialGroupNotFound` |
| 46 | - ❌ `Unauthorized` |
| 47 | - ❌ `AccessDenied` |
| 48 | |
| 49 | ## Evidence |
| 50 | - Enforced in CLAUDE.md development standards |
| 51 | - Consistent pattern across all services |
| 52 |