HttpException

Defines the base Nest HTTP exception, which is handled by the default Exceptions Handler.

  
class HttpException extends Error {
  static createBody(objectOrErrorMessage: string | object, description?: string, statusCode?: number)
  static getDescriptionFrom(descriptionOrOptions: string | HttpExceptionOptions): string
  static getHttpExceptionOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): HttpExceptionOptions
  static extractDescriptionAndOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): DescriptionAndOptions
  constructor(response: string | Record<string, any>, status: number, options?: HttpExceptionOptions)
  cause: Error | undefined
  initCause(): void
  initMessage()
  initName(): void
  getResponse(): string | object
  getStatus(): number
}

See also

Static methods

createBody()


static createBody(objectOrErrorMessage: string | object, description?: string, statusCode?: number)

Parameters

Option Type Description
objectOrErrorMessage string | object
description string

Optional. Default is undefined.

statusCode number

Optional. Default is undefined.

getDescriptionFrom()


static getDescriptionFrom(descriptionOrOptions: string | HttpExceptionOptions): string

Parameters

Option Type Description
descriptionOrOptions string | HttpExceptionOptions

Returns

string

getHttpExceptionOptionsFrom()


static getHttpExceptionOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): HttpExceptionOptions

Parameters

Option Type Description
descriptionOrOptions string | HttpExceptionOptions

Returns

HttpExceptionOptions

extractDescriptionAndOptionsFrom()

Utility method used to extract the error description and httpExceptionOptions from the given argument. This is used by inheriting classes to correctly parse both options.


static extractDescriptionAndOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): DescriptionAndOptions

Parameters

Option Type Description
descriptionOrOptions string | HttpExceptionOptions

Returns

DescriptionAndOptions the error description and the httpExceptionOptions as an object.

Constructor

Instantiate a plain HTTP Exception.


constructor(response: string | Record<string, any>, status: number, options?: HttpExceptionOptions)

Parameters

Option Type Description
response string | Record

string, object describing the error condition or the error cause.

status number

HTTP response status code.

options HttpExceptionOptions

An object used to add an error cause.


Optional. Default is `undefined`.

Examples

    
throw new HttpException()
throw new HttpException('message', HttpStatus.BAD_REQUEST)
throw new HttpException({ reason: 'this can be a human readable reason' }, HttpStatus.BAD_REQUEST)
throw new HttpException(new Error('Cause Error'), HttpStatus.BAD_REQUEST)
throw new HttpException('custom message', HttpStatus.BAD_REQUEST, {
 cause: new Error('Cause Error'),
})

Usage Notes

The constructor arguments define the response and the HTTP response status code.

  • The response argument (required) defines the JSON response body. alternatively, it can also be an error object that is used to define an error cause.
  • The status argument (required) defines the HTTP Status Code.
  • The options argument (optional) defines additional error options. Currently, it supports the cause attribute, and can be used as an alternative way to specify the error cause: const error = new HttpException('description', 400, { cause: new Error() });

By default, the JSON response body contains two properties:

  • statusCode: the Http Status Code.
  • message: a short description of the HTTP error by default; override this by supplying a string in the response parameter.

To override the entire JSON response body, pass an object to the createBody method. Nest will serialize the object and return it as the JSON response body.

The status argument is required, and should be a valid HTTP status code. Best practice is to use the HttpStatus enum imported from nestjs/common.

Properties

Property Description
cause: Error | undefined

Methods

initCause()

Configures error chaining support


initCause(): void

Parameters

There are no parameters.

Returns

void

See:

initMessage()


initMessage()

Parameters

There are no parameters.

initName()


initName(): void

Parameters

There are no parameters.

Returns

void

getResponse()


getResponse(): string | object

Parameters

There are no parameters.

Returns

string | object

getStatus()


getStatus(): number

Parameters

There are no parameters.

Returns

number