Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从NestJS中的类验证器返回自定义响应_Javascript_Typescript_Nestjs_Class Validator - Fatal编程技术网

Javascript 如何从NestJS中的类验证器返回自定义响应

Javascript 如何从NestJS中的类验证器返回自定义响应,javascript,typescript,nestjs,class-validator,Javascript,Typescript,Nestjs,Class Validator,是否可以从NestJs内部的类验证器返回自定义错误响应 NestJS当前返回如下错误消息: { "statusCode": 400, "error": "Bad Request", "message": [ { "target": {}, "property": "username", "children": [], "constraints": {

是否可以从NestJs内部的类验证器返回自定义错误响应

NestJS当前返回如下错误消息:

{
    "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"
            }
        },
    ]
}
但是,使用我的API的服务需要更类似于:

{
    "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"
        }
    }
}

Nestjs具有名为异常过滤器的内置组件,如果您想在异常情况下修饰您的响应。你可以找到相关的文件

下面的代码片段可能有助于编写自己的过滤器

<!-- language: lang-typescript -->
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,
      });
  }
}

从'@nestjs/common'导入{ExceptionFilter,Catch,ArgumentsHost,BadRequestException};
从“express”导入{Request,Response};
@捕获(BadRequestException)
导出类BadRequestExceptionFilter实现ExceptionFilter{
捕获(异常:BadRequestException,主机:ArgumentsHost){
const ctx=host.switchToHttp();
const response=ctx.getResponse();
const request=ctx.getRequest();
const status=exception.getStatus();
响应
.地位(地位)
//您可以在这里操纵响应
.json({
状态代码:状态,
时间戳:新日期().toISOString(),
路径:request.url,
});
}
}