HttpException

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

  
class HttpException extends IntrinsicException {
  static createBody<Body extends Record<string, unknown>>(arg0: HttpExceptionBodyMessage | Body, arg1?: HttpExceptionBodyMessage, statusCode?: number): HttpExceptionBody | Body
  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: unknown
  initCause(): void
  initMessage()
  initName(): void
  getResponse(): string | object
  getStatus(): number

  // inherited from nest/packages/common/IntrinsicException
}

See also

Static methods

createBody()

Overload #1

static createBody(nil: "", message: HttpExceptionBodyMessage, statusCode: number): HttpExceptionBody

Parameters

Option Type Description
nil ""
message HttpExceptionBodyMessage
statusCode number

Returns

HttpExceptionBody


Overload #2

static createBody(message: HttpExceptionBodyMessage, error: string, statusCode: number): HttpExceptionBody

Parameters

Option Type Description
message HttpExceptionBodyMessage
error string
statusCode number

Returns

HttpExceptionBody


Overload #3

static createBody<Body extends Record<string, unknown>>(custom: Body): Body

Parameters

Option Type Description
custom Body

Returns

Body

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('message', 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: unknown

Exception cause. Indicates the specific original cause of the error. It is used when catching and re-throwing an error with a more-specific or useful error message in order to still have access to the original error.

Methods

initCause()

Configures error chaining support


initCause(): void

Parameters

There are no parameters.

Returns

void

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