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
| Exception | Status Code | Default Message |
|---|---|---|
BadRequestException | 400 | Bad Request |
UnauthorizedException | 401 | Unauthorized |
PaymentRequiredException | 402 | Payment Required |
ForbiddenException | 403 | Forbidden |
NotFoundException | 404 | Not Found |
MethodNotAllowedException | 405 | Method Not Allowed |
NotAcceptableException | 406 | Not Acceptable |
ConflictException | 409 | Conflict |
GoneException | 410 | Gone |
UnsupportedMediaTypeException | 415 | Unsupported Media Type |
UnprocessableEntityException | 422 | Unprocessable Entity |
TooManyRequestsException | 429 | Too Many Requests |
InternalServerErrorException | 500 | Internal Server Error |
NotImplementedException | 501 | Not Implemented |
BadGatewayException | 502 | Bad Gateway |
ServiceUnavailableException | 503 | Service Unavailable |
GatewayTimeoutException | 504 | Gateway Timeout |
Validation Errors
| Exception | Status Code | Description |
|---|---|---|
RequestValidationError | 404 | Thrown when @Body, @Path, @Query, or @File validation fails. |
ResponseValidationError | 500 | Thrown when responseModel validation fails. |
InvalidParamTypeError | — | Internal 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');