HttpException

Base class for HTTP error responses with a status code and message.

Since NestJS 10/11 Spec ↗

Syntax

new HttpException(response: string | object, status: number)

Parameters

NameTypeRequiredDescription
response string | object No The response body or message returned to the client.
status number No The HTTP status code for the error.

Returns

HttpException — An exception caught by the default exceptions filter.

Examples

import { Controller, Get, HttpException, HttpStatus, NotFoundException } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Get(':id')
  findOne() {
    throw new NotFoundException('Cat not found');
  }

  @Get('teapot')
  teapot() {
    throw new HttpException('I am a teapot', HttpStatus.I_AM_A_TEAPOT);
  }
}

Notes

Prefer the built-in subclasses (NotFoundException, BadRequestException, UnauthorizedException, ForbiddenException, ConflictException) for common cases. They are caught by the global exceptions filter and serialized to a consistent JSON shape.