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.
typescript
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:
typescript
logError(error: AppError, req?: Request): voidUse the logWarning method to log warnings:
typescript
logWarning(message: string, req?: Request): voidUse the logDebug method to log debug information:
typescript
logDebug(message: string, req?: Request): voidUse the logInfo method to log general information:
typescript
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
Development mode:
JSON
{
"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:
JSON
{
"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 | |
Prisma P1001 | |
Prisma P1002 | |
504 GatewayTimeout | GatewayTimeout |
SequelizeTimeoutError |
