Commit 96af25b
committed
feat(backend): standardize RFC 7807 error envelopes (closes #254)
Replace ad-hoc `{success: false, error: ...}` responses across error-
producing middleware and select controllers with the canonical RFC
7807 ProblemDetails envelope emitted by the central error handler.
**Wire contract**
Every 4xx/5xx response is now:
```http
Content-Type: application/problem+json
```
```json
{
"type": "https://aethermint.io/problems/...",
"title": "Validation Error",
"status": 400,
"detail": "...",
"instance": "POST /api/auth/register",
"code": "VALIDATION_ERROR",
"success": false,
"requestId": "<uuid-v4>",
"timestamp": "ISO-8601",
"errors": [{ "field": "email", "message": "..." }],
"error": { /* deprecated legacy mirror */ }
}
```
**Acceptance criteria**
- [x] Standard error JSON schema (type, title, status, detail, instance)
- [x] Error code catalog (see backend/docs/ERROR_CATALOG.md) with stable
URIs and machine-readable codes (Issue #254, \#127)
- [x] Middleware that wraps all responses
(backend/src/middleware/errorHandler.ts)
- [x] Migration of existing error responses (hot path middleware +
representative controllers, see Migration scope below)
- [x] Tests verifying error format consistency
(backend/tests/middleware/errorHandler.test.ts, ~14 cases)
**Migration scope**
Middleware (all on the request hot path):
- validation.ts, validation.js (Joi + express-validator)
- auth.js (JWT, RBAC, permission gates)
- permissions.js, tenant.js (multi-tenant)
- security.ts (DDOS, bot detection, blacklist, geo/time)
- ipfsAuth.js, apiKey.js, rateLimiter.js (auth & throttling)
- sanitizer.ts, idempotency.ts
- shutdown.ts (`shutdownGuard` now emits RFC 7807 503)
Controllers (representative, AppError throwers):
- courseController.ts (every catch block delegates to the central
handler; handler signatures updated to accept `next`)
- bulkOperationsController.ts (inline 400s → ValidationError with
field-level details)
- holographicController.ts, analyticsController.js (catch blocks)
**Backward compatibility**
The central handler still mirrors the legacy
`{success:false, error:{...}}` shape under body.error for older CLIs
that depend on it. The mirror is `deprecated: true` in OpenAPI and
scheduled for removal in the next major version. See
`backend/docs/ERROR_CATALOG.md` §4 for the field-mapping table.
**OpenAPI**
`backend/src/config/swagger.ts` and `backend/src/docs/openapi.ts`
now register:
- Component schemas: `ProblemDetails`, `FieldValidationError`,
legacy `Error` (deprecated).
- Reusable responses: `Problem400/401/403/404/409/413/415/429/500/503`.
- All paths use these references for error documentation.
**Tests**
- backend/tests/middleware/errorHandler.test.ts (new, 14 cases):
contract, content-type, catalog routing, validation details,
headersSent guard, unknown-code fallback, catchAsync plumbing,
legacy mirror, instance format, dev-only stack traces.
- backend/tests/middleware/auth.test.js: updated IPFS auth
expectations from `res.status().json({success:false,...})` to
`next(new AppError(...))` so existing assertions still pass.
Refs: #254 (RFC 7807), #127 (central error handler foundation)1 parent 1677837 commit 96af25b
24 files changed
Lines changed: 1650 additions & 1550 deletions
File tree
- backend
- docs
- src
- config
- controllers
- docs
- middleware
- utils
- tests/middleware
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
0 commit comments