@saihu/common 1.1.7 → 1.1.8
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/dist/filters/index.d.ts +1 -0
- package/dist/filters/index.d.ts.map +1 -1
- package/dist/filters/index.js +1 -0
- package/dist/filters/validation-exception.filter.d.ts +5 -0
- package/dist/filters/validation-exception.filter.d.ts.map +1 -0
- package/dist/filters/validation-exception.filter.js +40 -0
- package/package.json +1 -1
package/dist/filters/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC"}
|
package/dist/filters/index.js
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ExceptionFilter, ArgumentsHost, BadRequestException } from '@nestjs/common';
|
|
2
|
+
export declare class ValidationExceptionFilter implements ExceptionFilter {
|
|
3
|
+
catch(exception: BadRequestException, host: ArgumentsHost): void;
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=validation-exception.filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-exception.filter.d.ts","sourceRoot":"","sources":["../../src/filters/validation-exception.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,aAAa,EACb,mBAAmB,EAEpB,MAAM,gBAAgB,CAAC;AAGxB,qBACa,yBAA0B,YAAW,eAAe;IAC/D,KAAK,CAAC,SAAS,EAAE,mBAAmB,EAAE,IAAI,EAAE,aAAa;CA2B1D"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ValidationExceptionFilter = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
let ValidationExceptionFilter = class ValidationExceptionFilter {
|
|
12
|
+
catch(exception, host) {
|
|
13
|
+
const ctx = host.switchToHttp();
|
|
14
|
+
const response = ctx.getResponse();
|
|
15
|
+
const exceptionResponse = exception.getResponse();
|
|
16
|
+
// Check if it's a validation error (has array of messages)
|
|
17
|
+
if (exceptionResponse.message && Array.isArray(exceptionResponse.message)) {
|
|
18
|
+
// Format validation errors in your BaseResponse format
|
|
19
|
+
const errorResponse = {
|
|
20
|
+
code: 400, // BaseResponseDto.CODE_BAD_REQUEST
|
|
21
|
+
msg: exceptionResponse.message.join(', '),
|
|
22
|
+
data: null,
|
|
23
|
+
};
|
|
24
|
+
response.status(common_1.HttpStatus.BAD_REQUEST).json(errorResponse);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// Handle other BadRequestExceptions
|
|
28
|
+
const errorResponse = {
|
|
29
|
+
code: 400,
|
|
30
|
+
msg: exceptionResponse.message || 'Bad Request',
|
|
31
|
+
data: null,
|
|
32
|
+
};
|
|
33
|
+
response.status(common_1.HttpStatus.BAD_REQUEST).json(errorResponse);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
exports.ValidationExceptionFilter = ValidationExceptionFilter;
|
|
38
|
+
exports.ValidationExceptionFilter = ValidationExceptionFilter = __decorate([
|
|
39
|
+
(0, common_1.Catch)(common_1.BadRequestException)
|
|
40
|
+
], ValidationExceptionFilter);
|