admin管理员组文章数量:1226076
Is it possible to return a custom error response from class-validator inside of NestJs.
NestJS currently returns an error message like this:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {},
"property": "username",
"children": [],
"constraints": {
"maxLength": "username must be shorter than or equal to 20 characters",
"minLength": "username must be longer than or equal to 4 characters",
"isString": "username must be a string"
}
},
]
}
However the service that consumes my API needs something more akin to:
{
"status": 400,
"message": "Bad Request",
"success": false,
"meta": {
"details": {
"maxLength": "username must be shorter than or equal to 20 characters",
"minLength": "username must be longer than or equal to 4 characters",
"isString": "username must be a string"
}
}
}
Is it possible to return a custom error response from class-validator inside of NestJs.
NestJS currently returns an error message like this:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {},
"property": "username",
"children": [],
"constraints": {
"maxLength": "username must be shorter than or equal to 20 characters",
"minLength": "username must be longer than or equal to 4 characters",
"isString": "username must be a string"
}
},
]
}
However the service that consumes my API needs something more akin to:
{
"status": 400,
"message": "Bad Request",
"success": false,
"meta": {
"details": {
"maxLength": "username must be shorter than or equal to 20 characters",
"minLength": "username must be longer than or equal to 4 characters",
"isString": "username must be a string"
}
}
}
Share
Improve this question
asked Sep 6, 2019 at 10:44
KineticKinetic
1,7441 gold badge13 silver badges38 bronze badges
2 Answers
Reset to default 13Nestjs has built-in compoments named Exception filters, if you want to decorate your response in case of exceptions. You can find the relevant documentations here.
The following snippet could be helpful for writing your own filter.
import { ExceptionFilter, Catch, ArgumentsHost, BadRequestException } from '@nestjs/common';
import { Request, Response } from 'express';
@Catch(BadRequestException)
export class BadRequestExceptionFilter implements ExceptionFilter {
catch(exception: BadRequestException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();
response
.status(status)
// you can manipulate the response here
.json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
When you define ValidationPipe
you can provide exceptionFactory
.
new ValidationPipe({
exceptionFactory: (errors) => new HttpException({ error: "BAD_REQUEST" }, 400)
})
The first argument type is string, but you can provide object.
According to the NestJS documentation:
To override just the message portion of the JSON response body, supply a string in the response argument. To override the entire JSON response body, pass an object in the response argument. Nest will serialize the object and return it as the JSON response body.
本文标签: javascriptHow to return a custom response from the classvalidator in NestJSStack Overflow
版权声明:本文标题:javascript - How to return a custom response from the class-validator in NestJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739464388a2164294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论