nestjs-exception-handler 4.2.0 → 4.3.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 +30 -24
- package/dist/index.d.ts +30 -24
- package/dist/index.js +265 -218
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +263 -228
- 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,153 +226,12 @@ DtoValidationFormatter = __decorateClass([
|
|
|
182
226
|
(0, import_common2.Injectable)()
|
|
183
227
|
], DtoValidationFormatter);
|
|
184
228
|
|
|
185
|
-
// src/
|
|
229
|
+
// src/filter/global-exception.filter.ts
|
|
186
230
|
var import_common3 = require("@nestjs/common");
|
|
187
|
-
var OtherExceptionFormatter = class {
|
|
188
|
-
formatOtherError(exception) {
|
|
189
|
-
if (exception && typeof exception === "object" && "path" in exception && "message" in exception) {
|
|
190
|
-
return [
|
|
191
|
-
{
|
|
192
|
-
path: String(exception.path),
|
|
193
|
-
message: String(exception.message)
|
|
194
|
-
}
|
|
195
|
-
];
|
|
196
|
-
}
|
|
197
|
-
const message = exception && typeof exception === "object" && "message" in exception ? String(exception.message) : "An unexpected error occurred";
|
|
198
|
-
return [
|
|
199
|
-
{
|
|
200
|
-
path: "unknown",
|
|
201
|
-
message
|
|
202
|
-
}
|
|
203
|
-
];
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
OtherExceptionFormatter = __decorateClass([
|
|
207
|
-
(0, import_common3.Injectable)()
|
|
208
|
-
], OtherExceptionFormatter);
|
|
209
|
-
|
|
210
|
-
// src/exception-filter/global-exception.filter.ts
|
|
211
|
-
var import_common4 = require("@nestjs/common");
|
|
212
|
-
var import_library2 = require("@prisma/client/runtime/library");
|
|
213
231
|
var GlobalExceptionFilter = class {
|
|
214
|
-
constructor(prismaExceptionFormatter, dtoValidationFormatter, otherValidationFormatter) {
|
|
215
|
-
this.prismaExceptionFormatter = prismaExceptionFormatter;
|
|
216
|
-
this.dtoValidationFormatter = dtoValidationFormatter;
|
|
217
|
-
this.otherValidationFormatter = otherValidationFormatter;
|
|
218
|
-
this.logger = new import_common4.Logger(GlobalExceptionFilter.name);
|
|
219
|
-
}
|
|
220
|
-
getErrorMessage(exception) {
|
|
221
|
-
if (exception instanceof import_library2.PrismaClientKnownRequestError || exception instanceof import_library2.PrismaClientValidationError || exception instanceof import_library2.PrismaClientRustPanicError || exception instanceof import_library2.PrismaClientUnknownRequestError || exception instanceof import_library2.PrismaClientInitializationError) {
|
|
222
|
-
return "Database error";
|
|
223
|
-
}
|
|
224
|
-
if (exception instanceof import_common4.HttpException) {
|
|
225
|
-
return exception.message || "HTTP error";
|
|
226
|
-
}
|
|
227
|
-
return "Internal server error";
|
|
228
|
-
}
|
|
229
|
-
catch(exception, host) {
|
|
230
|
-
const ctx = host.switchToHttp();
|
|
231
|
-
const response = ctx.getResponse();
|
|
232
|
-
const request = ctx.getRequest();
|
|
233
|
-
const status = exception instanceof import_common4.HttpException ? exception.getStatus() : import_common4.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
234
|
-
let errorMessages = [{ path: "unknown", message: "Internal server error" }];
|
|
235
|
-
if (exception instanceof import_library2.PrismaClientKnownRequestError || exception instanceof import_library2.PrismaClientValidationError || exception instanceof import_library2.PrismaClientRustPanicError || exception instanceof import_library2.PrismaClientUnknownRequestError || exception instanceof import_library2.PrismaClientInitializationError) {
|
|
236
|
-
errorMessages = this.prismaExceptionFormatter.formatError(exception);
|
|
237
|
-
} else if (exception instanceof import_common4.HttpException) {
|
|
238
|
-
errorMessages = this.dtoValidationFormatter.formatDtoValidationException(
|
|
239
|
-
exception
|
|
240
|
-
);
|
|
241
|
-
} else {
|
|
242
|
-
errorMessages = this.otherValidationFormatter.formatOtherError(exception);
|
|
243
|
-
}
|
|
244
|
-
this.logger.error(
|
|
245
|
-
`${request.method} ${request.url}`,
|
|
246
|
-
JSON.stringify(errorMessages),
|
|
247
|
-
"ExceptionFilter"
|
|
248
|
-
);
|
|
249
|
-
response.status(status).json({
|
|
250
|
-
success: false,
|
|
251
|
-
message: this.getErrorMessage(exception),
|
|
252
|
-
errorMessages
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
};
|
|
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
|
-
|
|
266
|
-
// src/constants/default-messages.ts
|
|
267
|
-
var DEFAULT_PATH = "unknown";
|
|
268
|
-
|
|
269
|
-
// src/formatters/unknown-exception.formatter.ts
|
|
270
|
-
var UnknownExceptionFormatter = class {
|
|
271
|
-
supports(_exception) {
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
format(_exception) {
|
|
275
|
-
return [
|
|
276
|
-
{
|
|
277
|
-
path: DEFAULT_PATH,
|
|
278
|
-
message: "Internal server error"
|
|
279
|
-
}
|
|
280
|
-
];
|
|
281
|
-
}
|
|
282
|
-
message(_exception) {
|
|
283
|
-
return "Internal server error";
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
// src/services/exception-handler.service.ts
|
|
288
|
-
var ExceptionHandlerService = class {
|
|
289
|
-
constructor() {
|
|
290
|
-
this.formatters = [];
|
|
291
|
-
this.defaultFormatter = new UnknownExceptionFormatter();
|
|
292
|
-
}
|
|
293
|
-
registerFormatter(formatter) {
|
|
294
|
-
this.formatters.push(formatter);
|
|
295
|
-
}
|
|
296
|
-
getFormatter(exception) {
|
|
297
|
-
for (const formatter of this.formatters) {
|
|
298
|
-
if (formatter.supports(exception)) {
|
|
299
|
-
return formatter;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return this.defaultFormatter;
|
|
303
|
-
}
|
|
304
|
-
formatException(exception) {
|
|
305
|
-
const formatter = this.getFormatter(exception);
|
|
306
|
-
const errors = formatter.format(exception);
|
|
307
|
-
const message = formatter.message(exception);
|
|
308
|
-
return { errors, message };
|
|
309
|
-
}
|
|
310
|
-
formatErrors(exception) {
|
|
311
|
-
const formatter = this.getFormatter(exception);
|
|
312
|
-
return formatter.format(exception);
|
|
313
|
-
}
|
|
314
|
-
getErrorMessage(exception) {
|
|
315
|
-
const formatter = this.getFormatter(exception);
|
|
316
|
-
return formatter.message(exception);
|
|
317
|
-
}
|
|
318
|
-
getAllFormatters() {
|
|
319
|
-
return [...this.formatters];
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
ExceptionHandlerService = __decorateClass([
|
|
323
|
-
(0, import_common5.Injectable)()
|
|
324
|
-
], ExceptionHandlerService);
|
|
325
|
-
|
|
326
|
-
// src/filter/global-exception.filter.ts
|
|
327
|
-
var import_common6 = require("@nestjs/common");
|
|
328
|
-
var GlobalExceptionFilter2 = class {
|
|
329
232
|
constructor(exceptionHandlerService, config) {
|
|
330
233
|
this.exceptionHandlerService = exceptionHandlerService;
|
|
331
|
-
this.logger = new
|
|
234
|
+
this.logger = new import_common3.Logger(GlobalExceptionFilter.name);
|
|
332
235
|
this.config = config || { enableLogging: true, hideStackTrace: false };
|
|
333
236
|
}
|
|
334
237
|
catch(exception, host) {
|
|
@@ -336,22 +239,40 @@ var GlobalExceptionFilter2 = class {
|
|
|
336
239
|
const response = ctx.getResponse();
|
|
337
240
|
const request = ctx.getRequest();
|
|
338
241
|
const { errors, message } = this.exceptionHandlerService.formatException(exception);
|
|
339
|
-
|
|
242
|
+
let status = this.getStatusCode(exception);
|
|
243
|
+
let finalErrors = errors;
|
|
244
|
+
let finalMessage = message;
|
|
245
|
+
if (exception instanceof import_common3.NotFoundException) {
|
|
246
|
+
const exceptionResponse = exception.getResponse();
|
|
247
|
+
if (typeof exceptionResponse === "object" && exceptionResponse !== null && "message" in exceptionResponse) {
|
|
248
|
+
const msg = exceptionResponse.message;
|
|
249
|
+
if (typeof msg === "string" && msg.includes("Cannot")) {
|
|
250
|
+
status = import_common3.HttpStatus.NOT_FOUND;
|
|
251
|
+
finalMessage = "Route Not Found";
|
|
252
|
+
finalErrors = [
|
|
253
|
+
{
|
|
254
|
+
path: "route",
|
|
255
|
+
message: [msg]
|
|
256
|
+
}
|
|
257
|
+
];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
340
261
|
const errorResponse = {
|
|
341
262
|
success: false,
|
|
342
|
-
message,
|
|
343
|
-
errorMessages:
|
|
263
|
+
message: finalMessage,
|
|
264
|
+
errorMessages: finalErrors
|
|
344
265
|
};
|
|
345
266
|
if (this.config.enableLogging) {
|
|
346
|
-
this.logError(request, status,
|
|
267
|
+
this.logError(request, status, finalErrors, exception);
|
|
347
268
|
}
|
|
348
269
|
response.status(status).json(errorResponse);
|
|
349
270
|
}
|
|
350
271
|
getStatusCode(exception) {
|
|
351
|
-
if (exception instanceof
|
|
272
|
+
if (exception instanceof import_common3.HttpException) {
|
|
352
273
|
return exception.getStatus();
|
|
353
274
|
}
|
|
354
|
-
return
|
|
275
|
+
return import_common3.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
355
276
|
}
|
|
356
277
|
logError(request, status, errorMessages, exception) {
|
|
357
278
|
const method = request.method;
|
|
@@ -368,9 +289,65 @@ var GlobalExceptionFilter2 = class {
|
|
|
368
289
|
this.logger.error(`${method} ${url} - ${status}`, JSON.stringify(logData));
|
|
369
290
|
}
|
|
370
291
|
};
|
|
371
|
-
|
|
372
|
-
(0,
|
|
373
|
-
],
|
|
292
|
+
GlobalExceptionFilter = __decorateClass([
|
|
293
|
+
(0, import_common3.Catch)()
|
|
294
|
+
], GlobalExceptionFilter);
|
|
295
|
+
|
|
296
|
+
// src/module/exception-handler.module.ts
|
|
297
|
+
var import_common6 = require("@nestjs/common");
|
|
298
|
+
|
|
299
|
+
// src/services/exception-handler.service.ts
|
|
300
|
+
var import_common5 = require("@nestjs/common");
|
|
301
|
+
|
|
302
|
+
// src/formatters/http-exception.formatter.ts
|
|
303
|
+
var import_common4 = require("@nestjs/common");
|
|
304
|
+
var HttpExceptionFormatter = class {
|
|
305
|
+
supports(exception) {
|
|
306
|
+
return exception instanceof import_common4.HttpException;
|
|
307
|
+
}
|
|
308
|
+
format(exception) {
|
|
309
|
+
const httpException = exception;
|
|
310
|
+
const response = httpException.getResponse();
|
|
311
|
+
if (typeof response === "string") {
|
|
312
|
+
return [{ path: "http_error", message: [response] }];
|
|
313
|
+
}
|
|
314
|
+
if (typeof response === "object" && response !== null) {
|
|
315
|
+
const responseObj = response;
|
|
316
|
+
if (responseObj.message && Array.isArray(responseObj.message)) {
|
|
317
|
+
return [
|
|
318
|
+
{
|
|
319
|
+
path: "http_error",
|
|
320
|
+
message: responseObj.message
|
|
321
|
+
}
|
|
322
|
+
];
|
|
323
|
+
}
|
|
324
|
+
if (responseObj.message && typeof responseObj.message === "string") {
|
|
325
|
+
return [{ path: "http_error", message: [responseObj.message] }];
|
|
326
|
+
}
|
|
327
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
328
|
+
return [{ path: "http_error", message: [responseObj.error] }];
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return [{ path: "http_error", message: ["An error occurred"] }];
|
|
332
|
+
}
|
|
333
|
+
message(exception) {
|
|
334
|
+
const httpException = exception;
|
|
335
|
+
const response = httpException.getResponse();
|
|
336
|
+
if (typeof response === "string") {
|
|
337
|
+
return response;
|
|
338
|
+
}
|
|
339
|
+
if (typeof response === "object" && response !== null) {
|
|
340
|
+
const responseObj = response;
|
|
341
|
+
if (responseObj.message && typeof responseObj.message === "string") {
|
|
342
|
+
return responseObj.message;
|
|
343
|
+
}
|
|
344
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
345
|
+
return responseObj.error;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return "An error occurred";
|
|
349
|
+
}
|
|
350
|
+
};
|
|
374
351
|
|
|
375
352
|
// src/formatters/dto-exception.formatter.ts
|
|
376
353
|
var DtoExceptionFormatter = class {
|
|
@@ -391,7 +368,7 @@ var DtoExceptionFormatter = class {
|
|
|
391
368
|
if (children) {
|
|
392
369
|
return this.formatChildrenErrors(children);
|
|
393
370
|
}
|
|
394
|
-
return [{ path: "unknown", message: "Validation failed" }];
|
|
371
|
+
return [{ path: "unknown", message: ["Validation failed"] }];
|
|
395
372
|
}
|
|
396
373
|
message(exception) {
|
|
397
374
|
const error = exception;
|
|
@@ -413,10 +390,12 @@ var DtoExceptionFormatter = class {
|
|
|
413
390
|
return errors.flatMap((error) => {
|
|
414
391
|
const constraints = error.constraints;
|
|
415
392
|
if (constraints) {
|
|
416
|
-
return
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
393
|
+
return [
|
|
394
|
+
{
|
|
395
|
+
path: error.property,
|
|
396
|
+
message: Object.values(constraints)
|
|
397
|
+
}
|
|
398
|
+
];
|
|
420
399
|
}
|
|
421
400
|
return [];
|
|
422
401
|
});
|
|
@@ -425,63 +404,85 @@ var DtoExceptionFormatter = class {
|
|
|
425
404
|
return children.flatMap((child) => {
|
|
426
405
|
const constraints = child.constraints;
|
|
427
406
|
if (constraints) {
|
|
428
|
-
return
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
407
|
+
return [
|
|
408
|
+
{
|
|
409
|
+
path: child.property,
|
|
410
|
+
message: Object.values(constraints)
|
|
411
|
+
}
|
|
412
|
+
];
|
|
432
413
|
}
|
|
433
414
|
return [];
|
|
434
415
|
});
|
|
435
416
|
}
|
|
436
417
|
};
|
|
437
418
|
|
|
438
|
-
// src/
|
|
439
|
-
var
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
419
|
+
// src/constants/default-messages.ts
|
|
420
|
+
var DEFAULT_PATH = "unknown";
|
|
421
|
+
|
|
422
|
+
// src/formatters/unknown-exception.formatter.ts
|
|
423
|
+
var UnknownExceptionFormatter = class {
|
|
424
|
+
supports(_exception) {
|
|
425
|
+
return true;
|
|
443
426
|
}
|
|
444
|
-
format(
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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 }];
|
|
427
|
+
format(_exception) {
|
|
428
|
+
return [
|
|
429
|
+
{
|
|
430
|
+
path: DEFAULT_PATH,
|
|
431
|
+
message: ["Something went wrong"]
|
|
463
432
|
}
|
|
464
|
-
|
|
465
|
-
return [{ path: "unknown", message: "An error occurred" }];
|
|
433
|
+
];
|
|
466
434
|
}
|
|
467
|
-
message(
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
435
|
+
message(_exception) {
|
|
436
|
+
return "Internal Server Error";
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
// src/services/exception-handler.service.ts
|
|
441
|
+
var ExceptionHandlerService = class {
|
|
442
|
+
constructor() {
|
|
443
|
+
this.formatters = [];
|
|
444
|
+
this.defaultFormatter = new UnknownExceptionFormatter();
|
|
445
|
+
this.registerFormatters();
|
|
446
|
+
}
|
|
447
|
+
registerFormatters() {
|
|
448
|
+
this.formatters = [
|
|
449
|
+
new PrismaExceptionFormatter(),
|
|
450
|
+
new HttpExceptionFormatter(),
|
|
451
|
+
new DtoExceptionFormatter()
|
|
452
|
+
];
|
|
453
|
+
}
|
|
454
|
+
registerFormatter(formatter) {
|
|
455
|
+
this.formatters.push(formatter);
|
|
456
|
+
}
|
|
457
|
+
getFormatter(exception) {
|
|
458
|
+
for (const formatter of this.formatters) {
|
|
459
|
+
if (formatter.supports(exception)) {
|
|
460
|
+
return formatter;
|
|
480
461
|
}
|
|
481
462
|
}
|
|
482
|
-
return
|
|
463
|
+
return this.defaultFormatter;
|
|
464
|
+
}
|
|
465
|
+
formatException(exception) {
|
|
466
|
+
const formatter = this.getFormatter(exception);
|
|
467
|
+
const errors = formatter.format(exception);
|
|
468
|
+
const message = formatter.message(exception);
|
|
469
|
+
return { errors, message };
|
|
470
|
+
}
|
|
471
|
+
formatErrors(exception) {
|
|
472
|
+
const formatter = this.getFormatter(exception);
|
|
473
|
+
return formatter.format(exception);
|
|
474
|
+
}
|
|
475
|
+
getErrorMessage(exception) {
|
|
476
|
+
const formatter = this.getFormatter(exception);
|
|
477
|
+
return formatter.message(exception);
|
|
478
|
+
}
|
|
479
|
+
getAllFormatters() {
|
|
480
|
+
return [...this.formatters];
|
|
483
481
|
}
|
|
484
482
|
};
|
|
483
|
+
ExceptionHandlerService = __decorateClass([
|
|
484
|
+
(0, import_common5.Injectable)()
|
|
485
|
+
], ExceptionHandlerService);
|
|
485
486
|
|
|
486
487
|
// src/module/exception-handler.module.ts
|
|
487
488
|
var ExceptionHandlerModule = class {
|
|
@@ -489,9 +490,9 @@ var ExceptionHandlerModule = class {
|
|
|
489
490
|
const providers = [
|
|
490
491
|
ExceptionHandlerService,
|
|
491
492
|
{
|
|
492
|
-
provide:
|
|
493
|
+
provide: GlobalExceptionFilter,
|
|
493
494
|
useFactory: (service) => {
|
|
494
|
-
return new
|
|
495
|
+
return new GlobalExceptionFilter(service, config);
|
|
495
496
|
},
|
|
496
497
|
inject: [ExceptionHandlerService]
|
|
497
498
|
}
|
|
@@ -499,7 +500,7 @@ var ExceptionHandlerModule = class {
|
|
|
499
500
|
return {
|
|
500
501
|
module: ExceptionHandlerModule,
|
|
501
502
|
providers,
|
|
502
|
-
exports: [ExceptionHandlerService,
|
|
503
|
+
exports: [ExceptionHandlerService, GlobalExceptionFilter],
|
|
503
504
|
global: true
|
|
504
505
|
};
|
|
505
506
|
}
|
|
@@ -508,7 +509,7 @@ var ExceptionHandlerModule = class {
|
|
|
508
509
|
}
|
|
509
510
|
};
|
|
510
511
|
ExceptionHandlerModule = __decorateClass([
|
|
511
|
-
(0,
|
|
512
|
+
(0, import_common6.Module)({})
|
|
512
513
|
], ExceptionHandlerModule);
|
|
513
514
|
function initializeFormatters(service) {
|
|
514
515
|
service.registerFormatter(new PrismaExceptionFormatter());
|
|
@@ -516,13 +517,59 @@ function initializeFormatters(service) {
|
|
|
516
517
|
service.registerFormatter(new HttpExceptionFormatter());
|
|
517
518
|
service.registerFormatter(new UnknownExceptionFormatter());
|
|
518
519
|
}
|
|
520
|
+
|
|
521
|
+
// src/utils/http-error.formatter.ts
|
|
522
|
+
var HttpErrorFormatter = class {
|
|
523
|
+
formatHttpException(exception) {
|
|
524
|
+
const response = exception.getResponse();
|
|
525
|
+
if (typeof response === "string") {
|
|
526
|
+
return [{ path: "http_error", message: [response] }];
|
|
527
|
+
}
|
|
528
|
+
if (typeof response === "object" && response !== null) {
|
|
529
|
+
const responseObj = response;
|
|
530
|
+
if (Array.isArray(responseObj.message)) {
|
|
531
|
+
return [
|
|
532
|
+
{
|
|
533
|
+
path: "http_error",
|
|
534
|
+
message: responseObj.message
|
|
535
|
+
}
|
|
536
|
+
];
|
|
537
|
+
}
|
|
538
|
+
if (typeof responseObj.message === "string") {
|
|
539
|
+
return [{ path: "http_error", message: [responseObj.message] }];
|
|
540
|
+
}
|
|
541
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
542
|
+
return [{ path: "http_error", message: [responseObj.error] }];
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return [{ path: "http_error", message: ["An error occurred"] }];
|
|
546
|
+
}
|
|
547
|
+
getMessage(exception) {
|
|
548
|
+
const response = exception.getResponse();
|
|
549
|
+
if (typeof response === "string") {
|
|
550
|
+
return response;
|
|
551
|
+
}
|
|
552
|
+
if (typeof response === "object" && response !== null) {
|
|
553
|
+
const responseObj = response;
|
|
554
|
+
if (typeof responseObj.message === "string") {
|
|
555
|
+
return responseObj.message;
|
|
556
|
+
}
|
|
557
|
+
if (responseObj.error && typeof responseObj.error === "string") {
|
|
558
|
+
return responseObj.error;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return "An error occurred";
|
|
562
|
+
}
|
|
563
|
+
};
|
|
519
564
|
// Annotate the CommonJS export names for ESM import in node:
|
|
520
565
|
0 && (module.exports = {
|
|
521
566
|
DtoValidationFormatter,
|
|
522
567
|
ExceptionHandlerModule,
|
|
568
|
+
ExceptionHandlerService,
|
|
523
569
|
GlobalExceptionFilter,
|
|
524
|
-
|
|
570
|
+
HttpErrorFormatter,
|
|
525
571
|
PrismaExceptionFormatter,
|
|
572
|
+
ValidationErrorFormatter,
|
|
526
573
|
initializeFormatters
|
|
527
574
|
});
|
|
528
575
|
//# sourceMappingURL=index.js.map
|