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.

MethodStatus CodeDescription
Errors.BadRequest(message)400Bad Request
Errors.Unauthorized(message)401Unauthorized
Errors.PaymentRequired(message)402Payment Required
Errors.Forbidden(message)403Forbidden
Errors.NotFound(message)404Not Found
Errors.Conflict(message)409Conflict
Errors.UnprocessableContent(message)422Unprocessable Content
Errors.TooManyRequests(message)429TooManyRequests
Errors.InternalServerError(message)500Internal Server Error
Errors.NotImplemented(message)501Not Implemented
Errors.BadGateway(message)502Bad Gateway
Errors.ServiceUnavailable(message)503Service Unavailable
Errors.GatewayTimeout(message)504Gateway 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 errors
  • warning - for logging warnings
  • info - for general informational messages
  • debug - for detailed debugging information

Use the logError method to log errors:

typescript
logError(error: AppError, req?: Request): void

Use the logWarning method to log warnings:

typescript
logWarning(message: string, req?: Request): void

Use the logDebug method to log debug information:

typescript
logDebug(message: string, req?: Request): void

Use the logInfo method to log general information:

typescript
logInfo(message: string): void

Graceful shutdown & Global Handlers:

Read more about graceful shutdown

Configuration

Read more about configuration

Environment variables that affect library behavior:

  • NODE_ENV:
    • development: Response includes stack trace.
    • production: stack trace, url, method are hidden.
  • DEBUG: Set to true to 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 ErrorError Name
400 BadRequestBadRequest
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 UnauthorizedJsonWebTokenError
TokenExpiredError
NotBeforeError
UnauthorizedError
Unauthorized
402 PaymentRequiredPaymentRequired
403 ForbiddenForbidden
ForbiddenError
404 NotFoundNotFound
NotFoundError
SequelizeEmptyResultError
Prisma P2001
Prisma P2015
Prisma P2025
409 ConflictConflict
ConflictError
DuplicateKeyError
SequelizeForeignKeyConstraintError
SequelizeUniqueConstraintError
SequelizeOptimisticLockError
Prisma P2002
422 Unprocessable ContentFieldValidationError
GroupedAlternativeValidationError
AlternativeValidationError
429 TooManyRequestsTooManyRequests
TooManyRequestsError
RateLimitError
500 InternalServerErrorInternalServerError
SyntaxError
SequelizeDatabaseError
Prisma P2021
Prisma P2022
Prisma P2027
Prisma P1003
501 NotImplementedNotImplemented
502 BadGatewayBadGateway
503 ServiceUnavailableServiceUnavailable
SequelizeConnectionError
Prisma P1001
Prisma P1002
504 GatewayTimeoutGatewayTimeout
SequelizeTimeoutError