Api Reference


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

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)

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).

Configuration Options

Passed to setConfig(options):

OptionTypeDescriptionDefault
formatErrorfunctionCustomize the JSON response structure.
formatError: (err, {req, isDev}) => ({ 
    status: err.isOperational ? 'fail' : 'error',
    message: err.message,
    ...(isDev ? { 
        method: req.method,
        url: req.originalUrl,
        stack: err.stack
    } : {})
})
devEnvironmentsstring[]List of environments where stack traces, url, method are visible.
devEnvironments: ['development', 'dev']
customMappersfunction[]Array of functions to handle specific errors before the default logic.
[]
customLoggerObjectExternal logger (pino/winston) for custom logging
null

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
DuplicateKeyError
SequelizeForeignKeyConstraintError
SequelizeUniqueConstraintError
SequelizeValidationError
PrismaClientKnownRequestError
PrismaClientUnknownRequestError
SyntaxError (body-parser)*
401 UnauthorizedJsonWebTokenError
TokenExpiredError
NotBeforeError
UnauthorizedError
Unauthorized
402 PaymentRequiredPaymentRequired
403 ForbiddenForbidden
ForbiddenError
404 NotFoundNotFound
NotFoundError
409 ConflictConflict
ConflictError
422 Unprocessable ContentFieldValidationError
GroupedAlternativeValidationError
AlternativeValidationError
429 TooManyRequestsTooManyRequests
TooManyRequestsError
RateLimitError
500 InternalServerErrorInternalServerError
SyntaxError
501 NotImplementedNotImplemented
502 BadGatewayBadGateway
503 ServiceUnavailableServiceUnavailable