UNPKG

3.89 kBTypeScriptView Raw
1import { HttpExceptionBody, HttpExceptionBodyMessage } from '../interfaces/http/http-exception-body.interface';
2export interface HttpExceptionOptions {
3 /** original cause of the error */
4 cause?: unknown;
5 description?: string;
6}
7export interface DescriptionAndOptions {
8 description?: string;
9 httpExceptionOptions?: HttpExceptionOptions;
10}
11/**
12 * Defines the base Nest HTTP exception, which is handled by the default
13 * Exceptions Handler.
14 *
15 * @see [Built-in HTTP exceptions](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)
16 *
17 * @publicApi
18 */
19export declare class HttpException extends Error {
20 private readonly response;
21 private readonly status;
22 private readonly options?;
23 /**
24 * Instantiate a plain HTTP Exception.
25 *
26 * @example
27 * throw new HttpException('message', HttpStatus.BAD_REQUEST)
28 * throw new HttpException('custom message', HttpStatus.BAD_REQUEST, {
29 * cause: new Error('Cause Error'),
30 * })
31 *
32 *
33 * @usageNotes
34 * The constructor arguments define the response and the HTTP response status code.
35 * - The `response` argument (required) defines the JSON response body. alternatively, it can also be
36 * an error object that is used to define an error [cause](https://nodejs.org/en/blog/release/v16.9.0/#error-cause).
37 * - The `status` argument (required) defines the HTTP Status Code.
38 * - The `options` argument (optional) defines additional error options. Currently, it supports the `cause` attribute,
39 * and can be used as an alternative way to specify the error cause: `const error = new HttpException('description', 400, { cause: new Error() });`
40 *
41 * By default, the JSON response body contains two properties:
42 * - `statusCode`: the Http Status Code.
43 * - `message`: a short description of the HTTP error by default; override this
44 * by supplying a string in the `response` parameter.
45 *
46 * To override the entire JSON response body, pass an object to the `createBody`
47 * method. Nest will serialize the object and return it as the JSON response body.
48 *
49 * The `status` argument is required, and should be a valid HTTP status code.
50 * Best practice is to use the `HttpStatus` enum imported from `nestjs/common`.
51 *
52 * @param response string, object describing the error condition or the error cause.
53 * @param status HTTP response status code.
54 * @param options An object used to add an error cause.
55 */
56 constructor(response: string | Record<string, any>, status: number, options?: HttpExceptionOptions);
57 cause: unknown;
58 /**
59 * Configures error chaining support
60 *
61 * @see https://nodejs.org/en/blog/release/v16.9.0/#error-cause
62 * @see https://github.com/microsoft/TypeScript/issues/45167
63 */
64 initCause(): void;
65 initMessage(): void;
66 initName(): void;
67 getResponse(): string | object;
68 getStatus(): number;
69 static createBody(nil: null | '', message: HttpExceptionBodyMessage, statusCode: number): HttpExceptionBody;
70 static createBody(message: HttpExceptionBodyMessage, error: string, statusCode: number): HttpExceptionBody;
71 static createBody<Body extends Record<string, unknown>>(custom: Body): Body;
72 static getDescriptionFrom(descriptionOrOptions: string | HttpExceptionOptions): string;
73 static getHttpExceptionOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): HttpExceptionOptions;
74 /**
75 * Utility method used to extract the error description and httpExceptionOptions from the given argument.
76 * This is used by inheriting classes to correctly parse both options.
77 * @returns the error description and the httpExceptionOptions as an object.
78 */
79 static extractDescriptionAndOptionsFrom(descriptionOrOptions: string | HttpExceptionOptions): DescriptionAndOptions;
80}