nestjs-exception-handler 4.2.0 → 4.4.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.
- package/README.md +296 -99
- package/dist/index.d.mts +31 -24
- package/dist/index.d.ts +31 -24
- package/dist/index.js +263 -210
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +264 -223
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -31,13 +31,49 @@ var src_exports = {};
|
|
|
31
31
|
__export(src_exports, {
|
|
32
32
|
DtoValidationFormatter: () => DtoValidationFormatter,
|
|
33
33
|
ExceptionHandlerModule: () => ExceptionHandlerModule,
|
|
34
|
+
ExceptionHandlerService: () => ExceptionHandlerService,
|
|
34
35
|
GlobalExceptionFilter: () => GlobalExceptionFilter,
|
|
35
|
-
|
|
36
|
+
HttpErrorFormatter: () => HttpErrorFormatter,
|
|
36
37
|
PrismaExceptionFormatter: () => PrismaExceptionFormatter,
|
|
38
|
+
ValidationErrorFormatter: () => ValidationErrorFormatter,
|
|
37
39
|
initializeFormatters: () => initializeFormatters
|
|
38
40
|
});
|
|
39
41
|
module.exports = __toCommonJS(src_exports);
|
|
40
42
|
|
|
43
|
+
// src/formatters/validation-error.formatter.ts
|
|
44
|
+
var ValidationErrorFormatter = class {
|
|
45
|
+
formatValidationErrors(exception) {
|
|
46
|
+
const response = exception.getResponse();
|
|
47
|
+
if (typeof response === "object" && response !== null) {
|
|
48
|
+
const responseObj = response;
|
|
49
|
+
if (Array.isArray(responseObj.message)) {
|
|
50
|
+
const messages = responseObj.message;
|
|
51
|
+
if (messages.length > 0 && typeof messages[0] === "object" && "property" in messages[0]) {
|
|
52
|
+
return this.formatNestedValidationErrors(messages);
|
|
53
|
+
}
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
path: "http_error",
|
|
57
|
+
message: messages
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
path: "http_error",
|
|
65
|
+
message: ["Validation failed"]
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
formatNestedValidationErrors(errors) {
|
|
70
|
+
return errors.map((error) => ({
|
|
71
|
+
path: error.property,
|
|
72
|
+
message: error.constraints ? Object.values(error.constraints) : ["Validation error"]
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
41
77
|
// src/formatters/prisma-exception.formatter.ts
|
|
42
78
|
var import_common = require("@nestjs/common");
|
|
43
79
|
var import_library = require("@prisma/client/runtime/library");
|
|
@@ -73,7 +109,7 @@ var PrismaExceptionFormatter = class {
|
|
|
73
109
|
return [
|
|
74
110
|
{
|
|
75
111
|
path: field,
|
|
76
|
-
message: `A record with this ${field} already exists.`
|
|
112
|
+
message: [`A record with this ${field} already exists.`]
|
|
77
113
|
}
|
|
78
114
|
];
|
|
79
115
|
}
|
|
@@ -82,7 +118,7 @@ var PrismaExceptionFormatter = class {
|
|
|
82
118
|
return [
|
|
83
119
|
{
|
|
84
120
|
path: fieldName || "field",
|
|
85
|
-
message: `The referenced ${fieldName || "record"} does not exist.`
|
|
121
|
+
message: [`The referenced ${fieldName || "record"} does not exist.`]
|
|
86
122
|
}
|
|
87
123
|
];
|
|
88
124
|
}
|
|
@@ -91,7 +127,7 @@ var PrismaExceptionFormatter = class {
|
|
|
91
127
|
return [
|
|
92
128
|
{
|
|
93
129
|
path: fieldName || "field",
|
|
94
|
-
message: `The value for ${fieldName || "field"} is invalid.`
|
|
130
|
+
message: [`The value for ${fieldName || "field"} is invalid.`]
|
|
95
131
|
}
|
|
96
132
|
];
|
|
97
133
|
}
|
|
@@ -100,7 +136,7 @@ var PrismaExceptionFormatter = class {
|
|
|
100
136
|
return [
|
|
101
137
|
{
|
|
102
138
|
path: fieldName || "field",
|
|
103
|
-
message: `The ${fieldName || "field"} field is required.`
|
|
139
|
+
message: [`The ${fieldName || "field"} field is required.`]
|
|
104
140
|
}
|
|
105
141
|
];
|
|
106
142
|
}
|
|
@@ -108,7 +144,7 @@ var PrismaExceptionFormatter = class {
|
|
|
108
144
|
return [
|
|
109
145
|
{
|
|
110
146
|
path: "record",
|
|
111
|
-
message: "The requested record does not exist."
|
|
147
|
+
message: ["The requested record does not exist."]
|
|
112
148
|
}
|
|
113
149
|
];
|
|
114
150
|
}
|
|
@@ -116,7 +152,7 @@ var PrismaExceptionFormatter = class {
|
|
|
116
152
|
return [
|
|
117
153
|
{
|
|
118
154
|
path: "database",
|
|
119
|
-
message: "Database operation failed."
|
|
155
|
+
message: ["Database operation failed."]
|
|
120
156
|
}
|
|
121
157
|
];
|
|
122
158
|
}
|
|
@@ -129,7 +165,7 @@ var PrismaExceptionFormatter = class {
|
|
|
129
165
|
return [
|
|
130
166
|
{
|
|
131
167
|
path: "database",
|
|
132
|
-
message
|
|
168
|
+
message: [message]
|
|
133
169
|
}
|
|
134
170
|
];
|
|
135
171
|
}
|
|
@@ -137,7 +173,7 @@ var PrismaExceptionFormatter = class {
|
|
|
137
173
|
return [
|
|
138
174
|
{
|
|
139
175
|
path: "database",
|
|
140
|
-
message: `Database initialization error: ${exception.message}`
|
|
176
|
+
message: [`Database initialization error: ${exception.message}`]
|
|
141
177
|
}
|
|
142
178
|
];
|
|
143
179
|
}
|
|
@@ -145,7 +181,7 @@ var PrismaExceptionFormatter = class {
|
|
|
145
181
|
return [
|
|
146
182
|
{
|
|
147
183
|
path: "unknown",
|
|
148
|
-
message: exception instanceof Error ? exception.message : "An unexpected database error occurred."
|
|
184
|
+
message: exception instanceof Error ? [exception.message] : ["An unexpected database error occurred."]
|
|
149
185
|
}
|
|
150
186
|
];
|
|
151
187
|
}
|
|
@@ -166,14 +202,22 @@ var DtoValidationFormatter = class {
|
|
|
166
202
|
const validationErrors = messages;
|
|
167
203
|
return validationErrors.map((error) => ({
|
|
168
204
|
path: error.property,
|
|
169
|
-
message: error.constraints ? Object.values(error.constraints)
|
|
205
|
+
message: error.constraints ? Object.values(error.constraints) : ["Validation error"]
|
|
170
206
|
}));
|
|
171
207
|
}
|
|
208
|
+
if (typeof firstMessage === "string") {
|
|
209
|
+
return [
|
|
210
|
+
{
|
|
211
|
+
path: "http_error",
|
|
212
|
+
message: messages
|
|
213
|
+
}
|
|
214
|
+
];
|
|
215
|
+
}
|
|
172
216
|
}
|
|
173
217
|
return [
|
|
174
218
|
{
|
|
175
219
|
path: "http_error",
|
|
176
|
-
message: typeof responseBody === "object" && responseBody !== null && "message" in responseBody ?
|
|
220
|
+
message: typeof responseBody === "object" && responseBody !== null && "message" in responseBody ? [responseBody.message] : ["An HTTP error occurred"]
|
|
177
221
|
}
|
|
178
222
|
];
|
|
179
223
|
}
|
|
@@ -182,86 +226,128 @@ DtoValidationFormatter = __decorateClass([
|
|
|
182
226
|
(0, import_common2.Injectable)()
|
|
183
227
|
], DtoValidationFormatter);
|
|
184
228
|
|
|
185
|
-
// src/
|
|
229
|
+
// src/filter/global-exception.filter.ts
|
|
230
|
+
var import_common5 = require("@nestjs/common");
|
|
231
|
+
|
|
232
|
+
// src/services/exception-handler.service.ts
|
|
233
|
+
var import_common4 = require("@nestjs/common");
|
|
234
|
+
|
|
235
|
+
// src/formatters/http-exception.formatter.ts
|
|
186
236
|
var import_common3 = require("@nestjs/common");
|
|
187
|
-
var
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
];
|
|
237
|
+
var HttpExceptionFormatter = class {
|
|
238
|
+
supports(exception) {
|
|
239
|
+
return exception instanceof import_common3.HttpException;
|
|
240
|
+
}
|
|
241
|
+
format(exception) {
|
|
242
|
+
const httpException = exception;
|
|
243
|
+
const response = httpException.getResponse();
|
|
244
|
+
if (typeof response === "string") {
|
|
245
|
+
return [{ path: "http_error", message: [response] }];
|
|
196
246
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
{
|
|
200
|
-
|
|
201
|
-
|
|
247
|
+
if (typeof response === "object" && response !== null) {
|
|
248
|
+
const responseObj = response;
|
|
249
|
+
if (responseObj.message && Array.isArray(responseObj.message)) {
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
path: "http_error",
|
|
253
|
+
message: responseObj.message
|
|
254
|
+
}
|
|
255
|
+
];
|
|
202
256
|
}
|
|
203
|
-
|
|
257
|
+
if (responseObj.message && typeof responseObj.message === "string") {
|
|
258
|
+
return [{ path: "http_error", message: [responseObj.message] }];
|
|
259
|
+
}
|
|
260
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
261
|
+
return [{ path: "http_error", message: [responseObj.error] }];
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return [{ path: "http_error", message: ["An error occurred"] }];
|
|
265
|
+
}
|
|
266
|
+
message(exception) {
|
|
267
|
+
const httpException = exception;
|
|
268
|
+
const response = httpException.getResponse();
|
|
269
|
+
if (typeof response === "string") {
|
|
270
|
+
return response;
|
|
271
|
+
}
|
|
272
|
+
if (typeof response === "object" && response !== null) {
|
|
273
|
+
const responseObj = response;
|
|
274
|
+
if (responseObj.message && typeof responseObj.message === "string") {
|
|
275
|
+
return responseObj.message;
|
|
276
|
+
}
|
|
277
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
278
|
+
return responseObj.error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return "An error occurred";
|
|
204
282
|
}
|
|
205
283
|
};
|
|
206
|
-
OtherExceptionFormatter = __decorateClass([
|
|
207
|
-
(0, import_common3.Injectable)()
|
|
208
|
-
], OtherExceptionFormatter);
|
|
209
284
|
|
|
210
|
-
// src/
|
|
211
|
-
var
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
this.logger = new import_common4.Logger(GlobalExceptionFilter.name);
|
|
285
|
+
// src/formatters/dto-exception.formatter.ts
|
|
286
|
+
var DtoExceptionFormatter = class {
|
|
287
|
+
supports(exception) {
|
|
288
|
+
if (!exception || typeof exception !== "object") {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
const error = exception;
|
|
292
|
+
return Array.isArray(error.validationErrors) || Array.isArray(error.children) && error.constraints !== void 0;
|
|
219
293
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
294
|
+
format(exception) {
|
|
295
|
+
const error = exception;
|
|
296
|
+
const validationErrors = error.validationErrors;
|
|
297
|
+
const children = error.children;
|
|
298
|
+
if (validationErrors) {
|
|
299
|
+
return this.formatValidationErrors(validationErrors);
|
|
223
300
|
}
|
|
224
|
-
if (
|
|
225
|
-
return
|
|
301
|
+
if (children) {
|
|
302
|
+
return this.formatChildrenErrors(children);
|
|
226
303
|
}
|
|
227
|
-
return "
|
|
304
|
+
return [{ path: "unknown", message: ["Validation failed"] }];
|
|
228
305
|
}
|
|
229
|
-
|
|
230
|
-
const
|
|
231
|
-
const
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
exception
|
|
240
|
-
);
|
|
241
|
-
} else {
|
|
242
|
-
errorMessages = this.otherValidationFormatter.formatOtherError(exception);
|
|
306
|
+
message(exception) {
|
|
307
|
+
const error = exception;
|
|
308
|
+
const validationErrors = error.validationErrors;
|
|
309
|
+
const children = error.children;
|
|
310
|
+
if (validationErrors && validationErrors.length > 0) {
|
|
311
|
+
const firstError = validationErrors[0];
|
|
312
|
+
const constraints = firstError.constraints;
|
|
313
|
+
if (constraints) {
|
|
314
|
+
return Object.values(constraints)[0];
|
|
315
|
+
}
|
|
243
316
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
317
|
+
if (children && children.length > 0) {
|
|
318
|
+
return "Validation failed for nested fields";
|
|
319
|
+
}
|
|
320
|
+
return "Validation failed";
|
|
321
|
+
}
|
|
322
|
+
formatValidationErrors(errors) {
|
|
323
|
+
return errors.flatMap((error) => {
|
|
324
|
+
const constraints = error.constraints;
|
|
325
|
+
if (constraints) {
|
|
326
|
+
return [
|
|
327
|
+
{
|
|
328
|
+
path: error.property,
|
|
329
|
+
message: Object.values(constraints)
|
|
330
|
+
}
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
return [];
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
formatChildrenErrors(children) {
|
|
337
|
+
return children.flatMap((child) => {
|
|
338
|
+
const constraints = child.constraints;
|
|
339
|
+
if (constraints) {
|
|
340
|
+
return [
|
|
341
|
+
{
|
|
342
|
+
path: child.property,
|
|
343
|
+
message: Object.values(constraints)
|
|
344
|
+
}
|
|
345
|
+
];
|
|
346
|
+
}
|
|
347
|
+
return [];
|
|
253
348
|
});
|
|
254
349
|
}
|
|
255
350
|
};
|
|
256
|
-
GlobalExceptionFilter = __decorateClass([
|
|
257
|
-
(0, import_common4.Catch)()
|
|
258
|
-
], GlobalExceptionFilter);
|
|
259
|
-
|
|
260
|
-
// src/module/exception-handler.module.ts
|
|
261
|
-
var import_common8 = require("@nestjs/common");
|
|
262
|
-
|
|
263
|
-
// src/services/exception-handler.service.ts
|
|
264
|
-
var import_common5 = require("@nestjs/common");
|
|
265
351
|
|
|
266
352
|
// src/constants/default-messages.ts
|
|
267
353
|
var DEFAULT_PATH = "unknown";
|
|
@@ -275,12 +361,12 @@ var UnknownExceptionFormatter = class {
|
|
|
275
361
|
return [
|
|
276
362
|
{
|
|
277
363
|
path: DEFAULT_PATH,
|
|
278
|
-
message: "
|
|
364
|
+
message: ["Something went wrong"]
|
|
279
365
|
}
|
|
280
366
|
];
|
|
281
367
|
}
|
|
282
368
|
message(_exception) {
|
|
283
|
-
return "Internal
|
|
369
|
+
return "Internal Server Error";
|
|
284
370
|
}
|
|
285
371
|
};
|
|
286
372
|
|
|
@@ -289,6 +375,14 @@ var ExceptionHandlerService = class {
|
|
|
289
375
|
constructor() {
|
|
290
376
|
this.formatters = [];
|
|
291
377
|
this.defaultFormatter = new UnknownExceptionFormatter();
|
|
378
|
+
this.registerFormatters();
|
|
379
|
+
}
|
|
380
|
+
registerFormatters() {
|
|
381
|
+
this.formatters = [
|
|
382
|
+
new PrismaExceptionFormatter(),
|
|
383
|
+
new HttpExceptionFormatter(),
|
|
384
|
+
new DtoExceptionFormatter()
|
|
385
|
+
];
|
|
292
386
|
}
|
|
293
387
|
registerFormatter(formatter) {
|
|
294
388
|
this.formatters.push(formatter);
|
|
@@ -320,38 +414,61 @@ var ExceptionHandlerService = class {
|
|
|
320
414
|
}
|
|
321
415
|
};
|
|
322
416
|
ExceptionHandlerService = __decorateClass([
|
|
323
|
-
(0,
|
|
417
|
+
(0, import_common4.Injectable)()
|
|
324
418
|
], ExceptionHandlerService);
|
|
325
419
|
|
|
326
420
|
// src/filter/global-exception.filter.ts
|
|
327
|
-
var
|
|
328
|
-
var GlobalExceptionFilter2 = class {
|
|
421
|
+
var GlobalExceptionFilter = class {
|
|
329
422
|
constructor(exceptionHandlerService, config) {
|
|
330
423
|
this.exceptionHandlerService = exceptionHandlerService;
|
|
331
|
-
this.logger = new
|
|
424
|
+
this.logger = new import_common5.Logger(GlobalExceptionFilter.name);
|
|
332
425
|
this.config = config || { enableLogging: true, hideStackTrace: false };
|
|
333
426
|
}
|
|
427
|
+
getService() {
|
|
428
|
+
if (!this.exceptionHandlerService) {
|
|
429
|
+
this.exceptionHandlerService = new ExceptionHandlerService();
|
|
430
|
+
}
|
|
431
|
+
return this.exceptionHandlerService;
|
|
432
|
+
}
|
|
334
433
|
catch(exception, host) {
|
|
335
434
|
const ctx = host.switchToHttp();
|
|
336
435
|
const response = ctx.getResponse();
|
|
337
436
|
const request = ctx.getRequest();
|
|
338
|
-
const { errors, message } = this.
|
|
339
|
-
|
|
437
|
+
const { errors, message } = this.getService().formatException(exception);
|
|
438
|
+
let status = this.getStatusCode(exception);
|
|
439
|
+
let finalErrors = errors;
|
|
440
|
+
let finalMessage = message;
|
|
441
|
+
if (exception instanceof import_common5.NotFoundException) {
|
|
442
|
+
const exceptionResponse = exception.getResponse();
|
|
443
|
+
if (typeof exceptionResponse === "object" && exceptionResponse !== null && "message" in exceptionResponse) {
|
|
444
|
+
const msg = exceptionResponse.message;
|
|
445
|
+
if (typeof msg === "string" && msg.includes("Cannot")) {
|
|
446
|
+
status = import_common5.HttpStatus.NOT_FOUND;
|
|
447
|
+
finalMessage = "Route Not Found";
|
|
448
|
+
finalErrors = [
|
|
449
|
+
{
|
|
450
|
+
path: "route",
|
|
451
|
+
message: [msg]
|
|
452
|
+
}
|
|
453
|
+
];
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
340
457
|
const errorResponse = {
|
|
341
458
|
success: false,
|
|
342
|
-
message,
|
|
343
|
-
errorMessages:
|
|
459
|
+
message: finalMessage,
|
|
460
|
+
errorMessages: finalErrors
|
|
344
461
|
};
|
|
345
462
|
if (this.config.enableLogging) {
|
|
346
|
-
this.logError(request, status,
|
|
463
|
+
this.logError(request, status, finalErrors, exception);
|
|
347
464
|
}
|
|
348
465
|
response.status(status).json(errorResponse);
|
|
349
466
|
}
|
|
350
467
|
getStatusCode(exception) {
|
|
351
|
-
if (exception instanceof
|
|
468
|
+
if (exception instanceof import_common5.HttpException) {
|
|
352
469
|
return exception.getStatus();
|
|
353
470
|
}
|
|
354
|
-
return
|
|
471
|
+
return import_common5.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
355
472
|
}
|
|
356
473
|
logError(request, status, errorMessages, exception) {
|
|
357
474
|
const method = request.method;
|
|
@@ -368,130 +485,20 @@ var GlobalExceptionFilter2 = class {
|
|
|
368
485
|
this.logger.error(`${method} ${url} - ${status}`, JSON.stringify(logData));
|
|
369
486
|
}
|
|
370
487
|
};
|
|
371
|
-
|
|
372
|
-
(0,
|
|
373
|
-
],
|
|
374
|
-
|
|
375
|
-
// src/formatters/dto-exception.formatter.ts
|
|
376
|
-
var DtoExceptionFormatter = class {
|
|
377
|
-
supports(exception) {
|
|
378
|
-
if (!exception || typeof exception !== "object") {
|
|
379
|
-
return false;
|
|
380
|
-
}
|
|
381
|
-
const error = exception;
|
|
382
|
-
return Array.isArray(error.validationErrors) || Array.isArray(error.children) && error.constraints !== void 0;
|
|
383
|
-
}
|
|
384
|
-
format(exception) {
|
|
385
|
-
const error = exception;
|
|
386
|
-
const validationErrors = error.validationErrors;
|
|
387
|
-
const children = error.children;
|
|
388
|
-
if (validationErrors) {
|
|
389
|
-
return this.formatValidationErrors(validationErrors);
|
|
390
|
-
}
|
|
391
|
-
if (children) {
|
|
392
|
-
return this.formatChildrenErrors(children);
|
|
393
|
-
}
|
|
394
|
-
return [{ path: "unknown", message: "Validation failed" }];
|
|
395
|
-
}
|
|
396
|
-
message(exception) {
|
|
397
|
-
const error = exception;
|
|
398
|
-
const validationErrors = error.validationErrors;
|
|
399
|
-
const children = error.children;
|
|
400
|
-
if (validationErrors && validationErrors.length > 0) {
|
|
401
|
-
const firstError = validationErrors[0];
|
|
402
|
-
const constraints = firstError.constraints;
|
|
403
|
-
if (constraints) {
|
|
404
|
-
return Object.values(constraints)[0];
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
if (children && children.length > 0) {
|
|
408
|
-
return "Validation failed for nested fields";
|
|
409
|
-
}
|
|
410
|
-
return "Validation failed";
|
|
411
|
-
}
|
|
412
|
-
formatValidationErrors(errors) {
|
|
413
|
-
return errors.flatMap((error) => {
|
|
414
|
-
const constraints = error.constraints;
|
|
415
|
-
if (constraints) {
|
|
416
|
-
return Object.entries(constraints).map(([, value]) => ({
|
|
417
|
-
path: error.property,
|
|
418
|
-
message: value
|
|
419
|
-
}));
|
|
420
|
-
}
|
|
421
|
-
return [];
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
formatChildrenErrors(children) {
|
|
425
|
-
return children.flatMap((child) => {
|
|
426
|
-
const constraints = child.constraints;
|
|
427
|
-
if (constraints) {
|
|
428
|
-
return Object.entries(constraints).map(([, value]) => ({
|
|
429
|
-
path: child.property,
|
|
430
|
-
message: value
|
|
431
|
-
}));
|
|
432
|
-
}
|
|
433
|
-
return [];
|
|
434
|
-
});
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
// src/formatters/http-exception.formatter.ts
|
|
439
|
-
var import_common7 = require("@nestjs/common");
|
|
440
|
-
var HttpExceptionFormatter = class {
|
|
441
|
-
supports(exception) {
|
|
442
|
-
return exception instanceof import_common7.HttpException;
|
|
443
|
-
}
|
|
444
|
-
format(exception) {
|
|
445
|
-
const httpException = exception;
|
|
446
|
-
const response = httpException.getResponse();
|
|
447
|
-
if (typeof response === "string") {
|
|
448
|
-
return [{ path: "unknown", message: response }];
|
|
449
|
-
}
|
|
450
|
-
if (typeof response === "object" && response !== null) {
|
|
451
|
-
const responseObj = response;
|
|
452
|
-
if (responseObj.message && Array.isArray(responseObj.message)) {
|
|
453
|
-
return responseObj.message.map((msg) => ({
|
|
454
|
-
path: "unknown",
|
|
455
|
-
message: msg
|
|
456
|
-
}));
|
|
457
|
-
}
|
|
458
|
-
if (responseObj.message && typeof responseObj.message === "string") {
|
|
459
|
-
return [{ path: "unknown", message: responseObj.message }];
|
|
460
|
-
}
|
|
461
|
-
if (responseObj.error && typeof responseObj.error === "string") {
|
|
462
|
-
return [{ path: "unknown", message: responseObj.error }];
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return [{ path: "unknown", message: "An error occurred" }];
|
|
466
|
-
}
|
|
467
|
-
message(exception) {
|
|
468
|
-
const httpException = exception;
|
|
469
|
-
const response = httpException.getResponse();
|
|
470
|
-
if (typeof response === "string") {
|
|
471
|
-
return response;
|
|
472
|
-
}
|
|
473
|
-
if (typeof response === "object" && response !== null) {
|
|
474
|
-
const responseObj = response;
|
|
475
|
-
if (responseObj.message && typeof responseObj.message === "string") {
|
|
476
|
-
return responseObj.message;
|
|
477
|
-
}
|
|
478
|
-
if (responseObj.error && typeof responseObj.error === "string") {
|
|
479
|
-
return responseObj.error;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
return "An error occurred";
|
|
483
|
-
}
|
|
484
|
-
};
|
|
488
|
+
GlobalExceptionFilter = __decorateClass([
|
|
489
|
+
(0, import_common5.Catch)()
|
|
490
|
+
], GlobalExceptionFilter);
|
|
485
491
|
|
|
486
492
|
// src/module/exception-handler.module.ts
|
|
493
|
+
var import_common6 = require("@nestjs/common");
|
|
487
494
|
var ExceptionHandlerModule = class {
|
|
488
495
|
static forRoot(config) {
|
|
489
496
|
const providers = [
|
|
490
497
|
ExceptionHandlerService,
|
|
491
498
|
{
|
|
492
|
-
provide:
|
|
499
|
+
provide: GlobalExceptionFilter,
|
|
493
500
|
useFactory: (service) => {
|
|
494
|
-
return new
|
|
501
|
+
return new GlobalExceptionFilter(service, config);
|
|
495
502
|
},
|
|
496
503
|
inject: [ExceptionHandlerService]
|
|
497
504
|
}
|
|
@@ -499,7 +506,7 @@ var ExceptionHandlerModule = class {
|
|
|
499
506
|
return {
|
|
500
507
|
module: ExceptionHandlerModule,
|
|
501
508
|
providers,
|
|
502
|
-
exports: [ExceptionHandlerService,
|
|
509
|
+
exports: [ExceptionHandlerService, GlobalExceptionFilter],
|
|
503
510
|
global: true
|
|
504
511
|
};
|
|
505
512
|
}
|
|
@@ -508,7 +515,7 @@ var ExceptionHandlerModule = class {
|
|
|
508
515
|
}
|
|
509
516
|
};
|
|
510
517
|
ExceptionHandlerModule = __decorateClass([
|
|
511
|
-
(0,
|
|
518
|
+
(0, import_common6.Module)({})
|
|
512
519
|
], ExceptionHandlerModule);
|
|
513
520
|
function initializeFormatters(service) {
|
|
514
521
|
service.registerFormatter(new PrismaExceptionFormatter());
|
|
@@ -516,13 +523,59 @@ function initializeFormatters(service) {
|
|
|
516
523
|
service.registerFormatter(new HttpExceptionFormatter());
|
|
517
524
|
service.registerFormatter(new UnknownExceptionFormatter());
|
|
518
525
|
}
|
|
526
|
+
|
|
527
|
+
// src/utils/http-error.formatter.ts
|
|
528
|
+
var HttpErrorFormatter = class {
|
|
529
|
+
formatHttpException(exception) {
|
|
530
|
+
const response = exception.getResponse();
|
|
531
|
+
if (typeof response === "string") {
|
|
532
|
+
return [{ path: "http_error", message: [response] }];
|
|
533
|
+
}
|
|
534
|
+
if (typeof response === "object" && response !== null) {
|
|
535
|
+
const responseObj = response;
|
|
536
|
+
if (Array.isArray(responseObj.message)) {
|
|
537
|
+
return [
|
|
538
|
+
{
|
|
539
|
+
path: "http_error",
|
|
540
|
+
message: responseObj.message
|
|
541
|
+
}
|
|
542
|
+
];
|
|
543
|
+
}
|
|
544
|
+
if (typeof responseObj.message === "string") {
|
|
545
|
+
return [{ path: "http_error", message: [responseObj.message] }];
|
|
546
|
+
}
|
|
547
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
548
|
+
return [{ path: "http_error", message: [responseObj.error] }];
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return [{ path: "http_error", message: ["An error occurred"] }];
|
|
552
|
+
}
|
|
553
|
+
getMessage(exception) {
|
|
554
|
+
const response = exception.getResponse();
|
|
555
|
+
if (typeof response === "string") {
|
|
556
|
+
return response;
|
|
557
|
+
}
|
|
558
|
+
if (typeof response === "object" && response !== null) {
|
|
559
|
+
const responseObj = response;
|
|
560
|
+
if (typeof responseObj.message === "string") {
|
|
561
|
+
return responseObj.message;
|
|
562
|
+
}
|
|
563
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
564
|
+
return responseObj.error;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return "An error occurred";
|
|
568
|
+
}
|
|
569
|
+
};
|
|
519
570
|
// Annotate the CommonJS export names for ESM import in node:
|
|
520
571
|
0 && (module.exports = {
|
|
521
572
|
DtoValidationFormatter,
|
|
522
573
|
ExceptionHandlerModule,
|
|
574
|
+
ExceptionHandlerService,
|
|
523
575
|
GlobalExceptionFilter,
|
|
524
|
-
|
|
576
|
+
HttpErrorFormatter,
|
|
525
577
|
PrismaExceptionFormatter,
|
|
578
|
+
ValidationErrorFormatter,
|
|
526
579
|
initializeFormatters
|
|
527
580
|
});
|
|
528
581
|
//# sourceMappingURL=index.js.map
|