nestjs-exception-handler 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +0,0 @@
1
- import { HttpException } from "@nestjs/common";
2
- export interface HttpErrorResponse {
3
- success: boolean;
4
- message: string;
5
- errorMessages: Array<{
6
- path: string;
7
- message: string;
8
- }>;
9
- }
10
- export interface ParsedHttpError {
11
- path: string;
12
- message: string;
13
- }
14
- export declare function parseHttpException(exception: HttpException): ParsedHttpError[];
15
- export declare function isNestJsHttpException(exception: unknown): boolean;
16
- export declare function getExceptionType(exception: unknown): string;
@@ -1,99 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseHttpException = parseHttpException;
4
- exports.isNestJsHttpException = isNestJsHttpException;
5
- exports.getExceptionType = getExceptionType;
6
- const common_1 = require("@nestjs/common");
7
- function parseHttpException(exception) {
8
- const response = exception.getResponse();
9
- const status = exception.getStatus();
10
- const message = exception.message;
11
- if (typeof response === "string") {
12
- return [
13
- {
14
- path: getPathForStatus(status),
15
- message: response || message,
16
- },
17
- ];
18
- }
19
- if (typeof response === "object" && response !== null) {
20
- const res = response;
21
- if (Array.isArray(res.message)) {
22
- return res.message.map((msg) => {
23
- if (typeof msg === "string") {
24
- return {
25
- path: res.path || getPathForStatus(status),
26
- message: msg,
27
- };
28
- }
29
- if (typeof msg === "object" && msg !== null) {
30
- const msgObj = msg;
31
- return {
32
- path: msgObj.path || msgObj.property || getPathForStatus(status),
33
- message: msgObj.message || JSON.stringify(msg),
34
- };
35
- }
36
- return {
37
- path: getPathForStatus(status),
38
- message: String(msg),
39
- };
40
- });
41
- }
42
- if (res.message && typeof res.message === "string") {
43
- return [
44
- {
45
- path: res.path || getPathForStatus(status),
46
- message: res.message,
47
- },
48
- ];
49
- }
50
- if (res.error && typeof res.error === "string") {
51
- return [
52
- {
53
- path: getPathForStatus(status),
54
- message: res.error,
55
- },
56
- ];
57
- }
58
- }
59
- return [
60
- {
61
- path: getPathForStatus(status),
62
- message: message || "HTTP error occurred",
63
- },
64
- ];
65
- }
66
- function getPathForStatus(status) {
67
- switch (status) {
68
- case 400:
69
- return "request";
70
- case 401:
71
- return "authentication";
72
- case 403:
73
- return "authorization";
74
- case 404:
75
- return "resource";
76
- case 409:
77
- return "conflict";
78
- default:
79
- return "request";
80
- }
81
- }
82
- function isNestJsHttpException(exception) {
83
- return exception instanceof common_1.HttpException;
84
- }
85
- function getExceptionType(exception) {
86
- if (exception instanceof common_1.BadRequestException)
87
- return "BadRequestException";
88
- if (exception instanceof common_1.UnauthorizedException)
89
- return "UnauthorizedException";
90
- if (exception instanceof common_1.ForbiddenException)
91
- return "ForbiddenException";
92
- if (exception instanceof common_1.NotFoundException)
93
- return "NotFoundException";
94
- if (exception instanceof common_1.ConflictException)
95
- return "ConflictException";
96
- if (exception instanceof common_1.HttpException)
97
- return "HttpException";
98
- return "Unknown";
99
- }
@@ -1,5 +0,0 @@
1
- export interface ParsedPrismaError {
2
- path: string;
3
- message: string;
4
- }
5
- export declare function parsePrismaError(exception: unknown): ParsedPrismaError[];
@@ -1,292 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parsePrismaError = parsePrismaError;
4
- const library_1 = require("@prisma/client/runtime/library");
5
- function parsePrismaError(exception) {
6
- if (exception instanceof library_1.PrismaClientKnownRequestError) {
7
- return parseKnownRequestError(exception);
8
- }
9
- if (exception instanceof library_1.PrismaClientValidationError) {
10
- return parseValidationError(exception);
11
- }
12
- if (exception instanceof library_1.PrismaClientInitializationError) {
13
- return parseInitializationError(exception);
14
- }
15
- return [];
16
- }
17
- function parseKnownRequestError(exception) {
18
- const code = exception.code;
19
- const meta = exception.meta;
20
- switch (code) {
21
- case "P2002": {
22
- const target = Array.isArray(meta === null || meta === void 0 ? void 0 : meta.target)
23
- ? meta.target[0]
24
- : (meta === null || meta === void 0 ? void 0 : meta.target) || "field";
25
- return [
26
- {
27
- path: target,
28
- message: "Unique constraint failed",
29
- },
30
- ];
31
- }
32
- case "P2025": {
33
- return [
34
- {
35
- path: (meta === null || meta === void 0 ? void 0 : meta.model_name) || "record",
36
- message: "Record not found",
37
- },
38
- ];
39
- }
40
- case "P2003": {
41
- return [
42
- {
43
- path: (meta === null || meta === void 0 ? void 0 : meta.field_name) || "relation",
44
- message: "Foreign key constraint failed",
45
- },
46
- ];
47
- }
48
- case "P2000": {
49
- return [
50
- {
51
- path: (meta === null || meta === void 0 ? void 0 : meta.field_name) || "field",
52
- message: `Value too long for column ${meta === null || meta === void 0 ? void 0 : meta.field_name}`,
53
- },
54
- ];
55
- }
56
- case "P2001": {
57
- return [
58
- {
59
- path: (meta === null || meta === void 0 ? void 0 : meta.field_name) || "record",
60
- message: "Record does not exist",
61
- },
62
- ];
63
- }
64
- case "P2004": {
65
- return [
66
- {
67
- path: "database",
68
- message: "A constraint failed on the database",
69
- },
70
- ];
71
- }
72
- case "P2005": {
73
- return [
74
- {
75
- path: (meta === null || meta === void 0 ? void 0 : meta.field_name) || "field",
76
- message: `Invalid value for field ${meta === null || meta === void 0 ? void 0 : meta.field_name}`,
77
- },
78
- ];
79
- }
80
- case "P2006": {
81
- return [
82
- {
83
- path: (meta === null || meta === void 0 ? void 0 : meta.field_name) || "field",
84
- message: `Invalid value for field ${meta === null || meta === void 0 ? void 0 : meta.field_name}`,
85
- },
86
- ];
87
- }
88
- case "P2007": {
89
- return [
90
- {
91
- path: "database",
92
- message: "Database error",
93
- },
94
- ];
95
- }
96
- case "P2008": {
97
- return [
98
- {
99
- path: "query",
100
- message: "Failed to parse query",
101
- },
102
- ];
103
- }
104
- case "P2009": {
105
- return [
106
- {
107
- path: "query",
108
- message: "Failed to validate query",
109
- },
110
- ];
111
- }
112
- case "P2010": {
113
- return [
114
- {
115
- path: "query",
116
- message: "Raw query failed",
117
- },
118
- ];
119
- }
120
- case "P2011": {
121
- return [
122
- {
123
- path: "database",
124
- message: "Null constraint violation",
125
- },
126
- ];
127
- }
128
- case "P2012": {
129
- return [
130
- {
131
- path: "database",
132
- message: "Missing a required value",
133
- },
134
- ];
135
- }
136
- case "P2013": {
137
- return [
138
- {
139
- path: "relation",
140
- message: "Missing the required relation",
141
- },
142
- ];
143
- }
144
- case "P2014": {
145
- return [
146
- {
147
- path: "relation",
148
- message: "Relation would be violated",
149
- },
150
- ];
151
- }
152
- case "P2015": {
153
- return [
154
- {
155
- path: "query",
156
- message: "Related record not found",
157
- },
158
- ];
159
- }
160
- case "P2016": {
161
- return [
162
- {
163
- path: "query",
164
- message: "Query interpretation error",
165
- },
166
- ];
167
- }
168
- case "P2017": {
169
- return [
170
- {
171
- path: "relation",
172
- message: "Records are not connected",
173
- },
174
- ];
175
- }
176
- case "P2018": {
177
- return [
178
- {
179
- path: "relation",
180
- message: "Required connected records not found",
181
- },
182
- ];
183
- }
184
- case "P2019": {
185
- return [
186
- {
187
- path: "input",
188
- message: "Input error",
189
- },
190
- ];
191
- }
192
- case "P2020": {
193
- return [
194
- {
195
- path: "value",
196
- message: "Value out of range",
197
- },
198
- ];
199
- }
200
- case "P2021": {
201
- return [
202
- {
203
- path: "table",
204
- message: `Table ${meta === null || meta === void 0 ? void 0 : meta.table} does not exist in the current database`,
205
- },
206
- ];
207
- }
208
- case "P2022": {
209
- return [
210
- {
211
- path: "column",
212
- message: `Column ${meta === null || meta === void 0 ? void 0 : meta.column} does not exist`,
213
- },
214
- ];
215
- }
216
- case "P2023": {
217
- return [
218
- {
219
- path: "database",
220
- message: "Database schema inconsistent",
221
- },
222
- ];
223
- }
224
- case "P2024": {
225
- return [
226
- {
227
- path: "database",
228
- message: "Timed out fetching a new connection from the connection pool",
229
- },
230
- ];
231
- }
232
- case "P2025": {
233
- return [
234
- {
235
- path: (meta === null || meta === void 0 ? void 0 : meta.model_name) || "record",
236
- message: "Record not found",
237
- },
238
- ];
239
- }
240
- case "P2026": {
241
- return [
242
- {
243
- path: "database",
244
- message: "Current provider does not support this feature",
245
- },
246
- ];
247
- }
248
- case "P2027": {
249
- return [
250
- {
251
- path: "database",
252
- message: "Multiple database errors",
253
- },
254
- ];
255
- }
256
- default: {
257
- return [
258
- {
259
- path: "database",
260
- message: exception.message || "Database error occurred",
261
- },
262
- ];
263
- }
264
- }
265
- }
266
- function parseValidationError(exception) {
267
- const message = exception.message;
268
- const match = message.match(/Argument `(\w+)` is missing/);
269
- if (match) {
270
- const field = match[1];
271
- return [
272
- {
273
- path: field,
274
- message: `Argument \`${field}\` is missing`,
275
- },
276
- ];
277
- }
278
- return [
279
- {
280
- path: "query",
281
- message: message || "Invalid query",
282
- },
283
- ];
284
- }
285
- function parseInitializationError(_exception) {
286
- return [
287
- {
288
- path: "database",
289
- message: "Failed to connect to the database",
290
- },
291
- ];
292
- }
@@ -1,10 +0,0 @@
1
- export interface ValidationError {
2
- property: string;
3
- constraints?: Record<string, string>;
4
- children?: ValidationError[];
5
- }
6
- export interface ParsedValidationError {
7
- path: string;
8
- message: string;
9
- }
10
- export declare function parseValidationErrors(errors: ValidationError[]): ParsedValidationError[];
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseValidationErrors = parseValidationErrors;
4
- function parseValidationErrors(errors) {
5
- const result = [];
6
- for (const error of errors) {
7
- if (error.constraints && Object.keys(error.constraints).length > 0) {
8
- const messages = Object.values(error.constraints).join(", ");
9
- result.push({
10
- path: error.property,
11
- message: messages,
12
- });
13
- }
14
- if (error.children && error.children.length > 0) {
15
- result.push(...parseValidationErrors(error.children));
16
- }
17
- }
18
- return result;
19
- }