Error Handling & RFC 9457
Shukoi enforces standard RFC 9457 Problem Details for HTTP APIs (application/problem+json) across all operational errors and domain exceptions.
The RFC 9457 Standard Envelope
All error responses—whether generated by schema validation failures, authentication rejections, payload size breaches, unmapped routes, or application exceptions—strictly follow this uniform JSON structure:
{
"type": "urn:shukoi:error:insufficient-funds",
"title": "INSUFFICIENT FUNDS",
"status": 402,
"detail": "Account balance $12.50 is lower than requested transfer $100.00.",
"instance": "/api/v1/transfers",
"code": "INSUFFICIENT_FUNDS",
"requestId": "req_01j7h8v1r5...",
"invalidParams": []
}Throwing Domain Errors (DomainError)
Business logic signals failures by throwing typed DomainError instances:
import { DomainError } from '@shukoi/core';
// Basic domain error with HTTP status code
throw new DomainError('USER_NOT_FOUND', 'Target user does not exist or has been deleted.', {
status: 404,
expose: true,
});
// Domain error with structured validation details
throw new DomainError('INSUFFICIENT_FUNDS', 'Balance is lower than requested charge amount.', {
status: 402,
expose: true,
details: {
currentBalance: 1250,
requestedAmount: 10000,
currency: 'USD',
},
});Constructor Signature
new DomainError(code: string, message: string, options?: DomainErrorOptions)| Option | Type | Default | Description |
|---|---|---|---|
status | number | 500 | HTTP status code associated with this domain failure (e.g. 400, 402, 404, 409). |
expose | boolean | true | If false, the message is masked as "Internal Server Error" in production environments. |
details | unknown | undefined | Arbitrary structured context attached to the problem details envelope. |
cause | unknown | undefined | Underlying causal error for chained error inspection and tracing. |
Built-In Error Categories & Status Codes
Shukoi automatically handles the following framework errors out-of-the-box:
| Condition | HTTP Status | Error Code (URN) | Description |
|---|---|---|---|
| Schema Validation Failure | 422 | urn:shukoi:error:validation-failed | Input payload or query parameters failed TypeBox schema rules. Contains detailed invalidParams array. |
| Authentication Missing | 401 | urn:shukoi:error:unauthorized | Action marked authRequired: true but request lacked valid Bearer token or API key. |
| Authorization Denied | 403 | urn:shukoi:error:forbidden | Caller identity lacks required permissions or Host header failed DNS-rebinding check. |
| Route Not Found | 404 | urn:shukoi:error:not-found | Request URL path does not map to any registered action route (handled via app.notFound()). |
| Payload Too Large | 413 | urn:shukoi:error:payload-too-large | Request body exceeded config.limits.maxBodyBytes. Stream aborted immediately without buffering. |
| Unhandled Exception | 500 | urn:shukoi:error:internal-error | Unexpected runtime crash. Stack traces are suppressed in production. |
Production Security: Zero Stack Trace Leakage
When NODE_ENV === 'production', internal execution details, database driver stack traces, and unexposed error messages are automatically scrubbed from the response envelope:
{
"type": "urn:shukoi:error:internal-error",
"title": "INTERNAL ERROR",
"status": 500,
"detail": "An internal server error occurred.",
"instance": "/api/v1/checkout",
"code": "INTERNAL_ERROR",
"requestId": "req_01j7h8v1r5..."
}The complete causal error and stack trace are logged securely to process.stderr tagged with the correlation requestId for monitoring and OpenTelemetry span attribution.