Exceptions

Nova provides a set of built-in HTTP exceptions that are automatically caught and returned as structured error responses.

HttpException

Base class for all HTTP exceptions. You can extend it or use it directly.

Signature TypeScript
class HttpException extends Error {
  constructor(
    statusCode: number,     // Required — HTTP status code
    message: string,         // Required — Error message
    detail?: any,            // Optional — Additional detail
  )
}

Usage

Throw any exception inside your controllers or services. Nova catches them and returns a structured response:

Example TypeScript
import { NotFoundException, BadRequestException } from '@abrahambass/nova';

@Get('/:id')
async findById(@Path('id') id: number) {
  const user = await this.userService.findById(id);

  if (!user) {
    throw new NotFoundException('User not found');
  }

  return user;
}

Response:

404 Response json
{
  "detail": "User not found"
}

Built-in Exceptions

ExceptionStatus CodeDefault Message
BadRequestException400Bad Request
UnauthorizedException401Unauthorized
PaymentRequiredException402Payment Required
ForbiddenException403Forbidden
NotFoundException404Not Found
MethodNotAllowedException405Method Not Allowed
NotAcceptableException406Not Acceptable
ConflictException409Conflict
GoneException410Gone
UnsupportedMediaTypeException415Unsupported Media Type
UnprocessableEntityException422Unprocessable Entity
TooManyRequestsException429Too Many Requests
InternalServerErrorException500Internal Server Error
NotImplementedException501Not Implemented
BadGatewayException502Bad Gateway
ServiceUnavailableException503Service Unavailable
GatewayTimeoutException504Gateway Timeout

Validation Errors

ExceptionStatus CodeDescription
RequestValidationError404Thrown when @Body, @Path, @Query, or @File validation fails.
ResponseValidationError500Thrown when responseModel validation fails.
InvalidParamTypeErrorInternal error for type casting failures (wrapped in RequestValidationError).

Custom Exceptions

Example TypeScript
import { HttpException } from '@abrahambass/nova';

class UserAlreadyExistsException extends HttpException {
  constructor(email: string) {
    super(409, 'Conflict', `User with email ${email} already exists`);
  }
}

// Usage
throw new UserAlreadyExistsException('john@mail.com');