UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.HttpException = void 0;
4const shared_utils_1 = require("../utils/shared.utils");
5/**
6 * Defines the base Nest HTTP exception, which is handled by the default
7 * Exceptions Handler.
8 *
9 * @see [Built-in HTTP exceptions](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)
10 *
11 * @publicApi
12 */
13class HttpException extends Error {
14 /**
15 * Instantiate a plain HTTP Exception.
16 *
17 * @example
18 * throw new HttpException('message', HttpStatus.BAD_REQUEST)
19 * throw new HttpException('custom message', HttpStatus.BAD_REQUEST, {
20 * cause: new Error('Cause Error'),
21 * })
22 *
23 *
24 * @usageNotes
25 * The constructor arguments define the response and the HTTP response status code.
26 * - The `response` argument (required) defines the JSON response body. alternatively, it can also be
27 * an error object that is used to define an error [cause](https://nodejs.org/en/blog/release/v16.9.0/#error-cause).
28 * - The `status` argument (required) defines the HTTP Status Code.
29 * - The `options` argument (optional) defines additional error options. Currently, it supports the `cause` attribute,
30 * and can be used as an alternative way to specify the error cause: `const error = new HttpException('description', 400, { cause: new Error() });`
31 *
32 * By default, the JSON response body contains two properties:
33 * - `statusCode`: the Http Status Code.
34 * - `message`: a short description of the HTTP error by default; override this
35 * by supplying a string in the `response` parameter.
36 *
37 * To override the entire JSON response body, pass an object to the `createBody`
38 * method. Nest will serialize the object and return it as the JSON response body.
39 *
40 * The `status` argument is required, and should be a valid HTTP status code.
41 * Best practice is to use the `HttpStatus` enum imported from `nestjs/common`.
42 *
43 * @param response string, object describing the error condition or the error cause.
44 * @param status HTTP response status code.
45 * @param options An object used to add an error cause.
46 */
47 constructor(response, status, options) {
48 super();
49 this.response = response;
50 this.status = status;
51 this.options = options;
52 this.initMessage();
53 this.initName();
54 this.initCause();
55 }
56 /**
57 * Configures error chaining support
58 *
59 * @see https://nodejs.org/en/blog/release/v16.9.0/#error-cause
60 * @see https://github.com/microsoft/TypeScript/issues/45167
61 */
62 initCause() {
63 if (this.options?.cause) {
64 this.cause = this.options.cause;
65 return;
66 }
67 }
68 initMessage() {
69 if ((0, shared_utils_1.isString)(this.response)) {
70 this.message = this.response;
71 }
72 else if ((0, shared_utils_1.isObject)(this.response) &&
73 (0, shared_utils_1.isString)(this.response.message)) {
74 this.message = this.response.message;
75 }
76 else if (this.constructor) {
77 this.message =
78 this.constructor.name.match(/[A-Z][a-z]+|[0-9]+/g)?.join(' ') ??
79 'Error';
80 }
81 }
82 initName() {
83 this.name = this.constructor.name;
84 }
85 getResponse() {
86 return this.response;
87 }
88 getStatus() {
89 return this.status;
90 }
91 static createBody(arg0, arg1, statusCode) {
92 if (!arg0) {
93 return {
94 message: arg1,
95 statusCode: statusCode,
96 };
97 }
98 if ((0, shared_utils_1.isString)(arg0) || Array.isArray(arg0)) {
99 return {
100 message: arg0,
101 error: arg1,
102 statusCode: statusCode,
103 };
104 }
105 return arg0;
106 }
107 static getDescriptionFrom(descriptionOrOptions) {
108 return (0, shared_utils_1.isString)(descriptionOrOptions)
109 ? descriptionOrOptions
110 : descriptionOrOptions?.description;
111 }
112 static getHttpExceptionOptionsFrom(descriptionOrOptions) {
113 return (0, shared_utils_1.isString)(descriptionOrOptions) ? {} : descriptionOrOptions;
114 }
115 /**
116 * Utility method used to extract the error description and httpExceptionOptions from the given argument.
117 * This is used by inheriting classes to correctly parse both options.
118 * @returns the error description and the httpExceptionOptions as an object.
119 */
120 static extractDescriptionAndOptionsFrom(descriptionOrOptions) {
121 const description = (0, shared_utils_1.isString)(descriptionOrOptions)
122 ? descriptionOrOptions
123 : descriptionOrOptions?.description;
124 const httpExceptionOptions = (0, shared_utils_1.isString)(descriptionOrOptions)
125 ? {}
126 : descriptionOrOptions;
127 return {
128 description,
129 httpExceptionOptions,
130 };
131 }
132}
133exports.HttpException = HttpException;