Skip to content

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:

json
{
  "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:

typescript
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 ​

typescript
new DomainError(code: string, message: string, options?: DomainErrorOptions)
OptionTypeDefaultDescription
statusnumber500HTTP status code associated with this domain failure (e.g. 400, 402, 404, 409).
exposebooleantrueIf false, the message is masked as "Internal Server Error" in production environments.
detailsunknownundefinedArbitrary structured context attached to the problem details envelope.
causeunknownundefinedUnderlying 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:

ConditionHTTP StatusError Code (URN)Description
Schema Validation Failure422urn:shukoi:error:validation-failedInput payload or query parameters failed TypeBox schema rules. Contains detailed invalidParams array.
Authentication Missing401urn:shukoi:error:unauthorizedAction marked authRequired: true but request lacked valid Bearer token or API key.
Authorization Denied403urn:shukoi:error:forbiddenCaller identity lacks required permissions or Host header failed DNS-rebinding check.
Route Not Found404urn:shukoi:error:not-foundRequest URL path does not map to any registered action route (handled via app.notFound()).
Payload Too Large413urn:shukoi:error:payload-too-largeRequest body exceeded config.limits.maxBodyBytes. Stream aborted immediately without buffering.
Unhandled Exception500urn:shukoi:error:internal-errorUnexpected 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:

json
{
  "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.

Released under the MIT License.