Skip to Content
Jumentix DocsReferenceErrors and Responses Map

Error Contracts and HTTP Error Responses

This document defines how errors are represented in code and serialized through HTTP adapters.

1) Base Error Contract

All domain/infra errors extend BaseError (apps/backend-template/src/infra/exceptions/BaseError.ts) and expose:

{
  name: string;
  code: string; // EErrorStringCodes
  message: string;
  correlationId: string;
  cause?: Error;
  metadata?: unknown;
}

2) Canonical Error Codes

Defined in apps/backend-template/src/infra/exceptions/error.codes.ts:

String CodeHTTP Status
GENERIC.INVALID_INPUT400
GENERIC.NOT_FOUND404
GENERIC.UNAUTHORIZED401
GENERIC.FORBIDDEN403
GENERIC.CONFLICT409
GENERIC.RESOURCE_LOCKED423
GENERIC.NOT_IMPLEMENTED501
GENERIC.INTERNAL_SERVER_ERROR500

Status mapping is resolved by toHttpStatus(...) in apps/backend-template/src/shared/utils.ts.

3) Error Class to Code Mapping

Error ClassNameCode
ValidationErrorvalidation_errorGENERIC.INVALID_INPUT
DomainValidationErrordomain_validation_errorGENERIC.INVALID_INPUT
ComposeEventErrorevent_invalid_messageGENERIC.INVALID_INPUT
DatabasePagingErrordatabase_paging_errorGENERIC.INVALID_INPUT
UnauthorizedErrorunauthorizedGENERIC.UNAUTHORIZED
ForbiddenErrorforbiddenGENERIC.FORBIDDEN
NotFoundErrornot_foundGENERIC.NOT_FOUND
DomainNotFoundErrordomain_not_foundGENERIC.NOT_FOUND
DataBaseNotFoundErrordatabase_not_foundGENERIC.NOT_FOUND
ConflictErrordatabase_duplicatedGENERIC.CONFLICT
ResourceLockedErrorlocked_resourceGENERIC.RESOURCE_LOCKED
NotImplementedinfrastructure_not_implementedGENERIC.NOT_IMPLEMENTED
InternalServerErrorinternal_server_errorGENERIC.INTERNAL_SERVER_ERROR

4) HTTP Error Response Shape

Current adapters serialize to the same payload shape:

{
  "message": "Human readable message",
  "error": {
    "name": "error_name",
    "code": "GENERIC.SOME_CODE",
    "message": "Original message",
    "correlationId": "..."
  }
}

Sources:

  • Express/Fastify/Restify: sendErrorResponse(...)
  • Lambda adapters: apps/backend-template/src/interface/HTTP/adapters/aws/lambda/responses/sendErrorResponse.ts

5) MessageMediator Request/Response Error Contract

For contract-based inter-service calls:

interface IMessageResponse<TResult = any> {
  contract: string;
  result?: TResult;
  error?: Error | Record<string, any>;
}

Errors are carried in the same response envelope (no throw required at transport boundary).

6) Current Consistency Notes

  1. Error status mapping is centralized (toHttpStatus).
  2. Human-readable formatting is centralized (formatErrorMessage).
  3. Correlation id is captured in BaseError from request context.
  4. New adapters should reuse existing sendErrorResponse semantics to preserve response contract consistency.

7) HTTP Request Validation Contract

All HTTP adapters use the same OpenAPI request-validation boundary:

  1. Unknown properties and required properties are checked first so existing public error messages remain stable.
  2. OpenAPI type, format, enum, range, and length constraints are then enforced.
  3. Schema-library diagnostics are translated when a stable domain-facing message already exists.
  4. createdAt and updatedAt are tolerated in update round trips as server-managed fields; they are not writable domain attributes.

Changes to request schemas or validation messages must be covered by the shared validator unit tests and the affected adapter integration suites.

8) Extension Rules

When creating a new custom error:

  1. Extend BaseError.
  2. Assign a stable name and code.
  3. Ensure code is covered in EErrorStringCodes/EErrorNumberCodes.
  4. Add formatErrorMessage branch if custom human-readable text is required.
  5. Keep HTTP and message-level error envelopes backward-compatible.

9) Realtime Error Envelopes

WebSocket (ApiResponse):

{
  "ok": false,
  "operationId": "createOrganization",
  "error": {
    "name": "validation_error",
    "message": "Invalid input data"
  }
}

gRPC (AsyncApiResponse):

{
  "ok": false,
  "operationId": "createOrganization",
  "errorName": "validation_error",
  "errorMessage": "Invalid input data"
}

See transport-specific contract references:

  • documentation/md/contracts/WEBSOCKET-REALTIME-CONTRACTS.md
  • documentation/md/contracts/GRPC-REALTIME-CONTRACTS.md