Judicial Backend uses a unified error-handling pipeline built on three layers: theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/BladimirGS/judicial-backend/llms.txt
Use this file to discover all available pages before exploring further.
AppError class that represents any anticipated failure, the asyncHandler wrapper that captures rejections from async controllers, and a global Express error middleware that classifies, logs, and serializes every error into a consistent JSON response. This approach ensures that no error reaches the client in an ambiguous or information-leaking form.
The AppError Class
AppError extends the native Error class and is the canonical way to signal expected failures — invalid input, missing records, unauthorized access, and similar scenarios. Because it is thrown intentionally, it is treated as an operational error: the server is functioning correctly, and the error is a legitimate response to a bad request or a missing resource.
| Field | Type | Description |
|---|---|---|
message | string | Human-readable description of the error (shown to the client for 4xx only). |
statusCode | number | HTTP status code; defaults to 500. |
code | string | Machine-readable identifier, e.g. APELACION_NOT_FOUND; defaults to INTERNAL_ERROR. |
details | unknown | Optional extra context (validation fields, debug hints). Optional. |
Throwing an AppError in a service
message, statusCode, and code. Pass a fourth argument when you need structured detail:
Operational vs Critical Errors
| Category | Source | Logged as | Client message |
|---|---|---|---|
| Operational | AppError instance | warn (4xx) / error (5xx) | Real message (4xx) or generic (5xx) |
| Critical | Unhandled Error or unknown value | error | "Error interno del servidor" |
Unhandled, non-operational errors never expose stack traces or internal details to the client in any environment. The stack is logged server-side via Pino, but the HTTP response always returns the generic string
"Error interno del servidor" with a 500 status.asyncHandler Wrapper
Every async controller must be wrapped with asyncHandler so that rejected promises are forwarded to Express’s error middleware rather than causing an unhandled rejection crash.
throw inside the wrapped function — including throw new AppError(...) — is caught by .catch(next) and passed to the global error handler automatically.
globalErrorHandler Middleware
Registered as the last middleware in the Express application, globalErrorHandler is the single exit point for all errors.
AppError4xx — logs awarnwithcode,message, anddetails; sends the real message to the client.AppError5xx — logs anerrorwith the stack; replaces the message with the generic fallback before sending.AppError400 +details— delegates toresponseUtil.validationErrorfor a structured validation response.- Native
Error— logsname,message, and stack; returns a 500 generic response. - Unknown value — logs the raw value; returns a 500 generic response.
responseUtil Helpers
All HTTP responses are produced through responseUtil to guarantee a consistent shape. The available helpers are:
Standard Error Response Shape
All error responses follow this JSON envelope:responseUtil.validationError is used (HTTP 400 with structured details), the body gains an errors array:
responseUtil.error helper sets code to "INTERNAL_SERVER_ERROR" for status 500, or "ERROR" for any other status code.