Api Reference
Have some ideas? Find something missing? or Bug?
Share your suggestions with us!Error Presets
All presets are available via the Errors object.
| Method | Status Code | Description |
|---|---|---|
Errors.BadRequest(message) | 400 | Bad Request |
Errors.Unauthorized(message) | 401 | Unauthorized |
Errors.PaymentRequired(message) | 402 | Payment Required |
Errors.Forbidden(message) | 403 | Forbidden |
Errors.NotFound(message) | 404 | Not Found |
Errors.Conflict(message) | 409 | Conflict |
Errors.UnprocessableContent(message) | 422 | Unprocessable Content |
Errors.TooManyRequests(message) | 429 | TooManyRequests |
Errors.InternalServerError(message) | 500 | Internal Server Error |
Errors.NotImplemented(message) | 501 | Not Implemented |
Errors.BadGateway(message) | 502 | Bad Gateway |
Errors.ServiceUnavailable(message) | 503 | Service Unavailable |
Errors.GatewayTimeout(message) | 504 | Gateway Timeout |
AppError Class
The base class for all errors. Use this if you need a custom status code.
new AppError(message: string, statusCode: number, isOperational: boolean = true)Logger
Built-in logger with four levels:
error- for logging errorswarning- for logging warningsinfo- for general informational messagesdebug- for detailed debugging information
Use the logError method to log errors:
logError(error: AppError, req?: Request): voidUse the logWarning method to log warnings:
logWarning(message: string, req?: Request): voidUse the logDebug method to log debug information:
logDebug(message: string, req?: Request): voidUse the logInfo method to log general information:
logInfo(message: string): voidGraceful shutdown & Global Handlers:
Read more about graceful shutdownConfiguration
Read more about configurationEnvironment variables that affect library behavior:
NODE_ENV:development: Response includesstacktrace.production:stacktrace,url,methodare hidden.DEBUG: Set totrueto enable debug logs (e.g., viewing raw error details before mapping).
Example Client Response
About Environment and Prod vs Dev Responses
If NODE_ENV is not defined, you will receive sanitised error messages, as the library behaves as if the environment were set to production.
To see how Production and Development responses differ, visit the Configuration part
See the devEnvironments property to customize which environments should display full error information (stack traces and full error messages)
Development mode:
{
"status": "error",
"method": "GET",
"url": "/api/users/999",
"message": "User not found",
"stack": "Error: User not found
at /app/controllers/user.js:15:20..."
}Production mode:
{
"status": "error",
"message": "User not found"
}Auto-Mapped Errors:
* Thrown by body-parser when JSON payload is malformed
| Throw Error | Error Name |
|---|---|
400 BadRequest | BadRequest |
UnknownFieldsError | |
ValidationError | |
ZodError | |
Joi Error | |
CastError | |
SequelizeValidationError | |
Prisma P2000 | |
Prisma P2003 | |
Prisma P2005 | |
Prisma P2006 | |
Prisma P2007 | |
Prisma P2011 | |
Prisma P2014 | |
SyntaxError (body-parser)* | |
401 Unauthorized | JsonWebTokenError |
TokenExpiredError | |
NotBeforeError | |
UnauthorizedError | |
Unauthorized | |
402 PaymentRequired | PaymentRequired |
403 Forbidden | Forbidden |
ForbiddenError | |
404 NotFound | NotFound |
NotFoundError | |
SequelizeEmptyResultError | |
Prisma P2001 | |
Prisma P2015 | |
Prisma P2025 | |
409 Conflict | Conflict |
ConflictError | |
DuplicateKeyError | |
SequelizeForeignKeyConstraintError | |
SequelizeUniqueConstraintError | |
SequelizeOptimisticLockError | |
Prisma P2002 | |
422 Unprocessable Content | FieldValidationError |
GroupedAlternativeValidationError | |
AlternativeValidationError | |
429 TooManyRequests | TooManyRequests |
TooManyRequestsError | |
RateLimitError | |
500 InternalServerError | InternalServerError |
SyntaxError | |
SequelizeDatabaseError | |
Prisma P2021 | |
Prisma P2022 | |
Prisma P2027 | |
Prisma P1003 | |
501 NotImplemented | NotImplemented |
502 BadGateway | BadGateway |
503 ServiceUnavailable | ServiceUnavailable |
SequelizeConnectionError | |
SequelizeAccessDeniedError | |
SequelizeHostNotReachableError | |
SequelizeHostNotFoundError | |
SequelizeConnectionRefusedError | |
Prisma P1001 | |
Prisma P1002 | |
504 GatewayTimeout | GatewayTimeout |
SequelizeTimeoutError |
ds-express-errors