【问题标题】:Why aren't NestJS GraphQL validation errors placed in the error message field?为什么 NestJS GraphQL 验证错误没有放在错误消息字段中?
【发布时间】:2020-07-17 15:06:41
【问题描述】:

我在意外的位置看到了一些类验证器错误。我希望有一种更方便的格式来处理错误,但也许我不知道一些用于处理扩展字段中对象的 graphQL 技巧......

在运行 NestJS sample/23-graphql-code-first 时,我在 GraphQL 操场上看到以下内容:

有输入:

  addRecipe(newRecipeData: {
    description: "too short"
    title: "this field should fail for being too long"
    ingredients: ["normal"]

  }) {
    title
  }
}

我回来了:

  "errors": [
    {
      "message": "Bad Request Exception",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addRecipe"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "response": {
            "statusCode": 400,
            "message": [
              "title must be shorter than or equal to 30 characters",
              "description must be longer than or equal to 30 characters"
            ],
            "error": "Bad Request"
          },
          "status": 400,
          "message": "Bad Request Exception",
          "stacktrace": [
            "Error: Bad Request Exception",
            "    at ValidationPipe.exceptionFactory nest/sample/23-graphql-code-first/node_modules/@nestjs/common/pipes/validation.pipe.js:78:20)",
          ...
          ]
        }
      }
    }
  ],
  "data": null
}

这些错误嵌套很深,“Bad Request Exception”没那么有用。这是否按预期工作?

【问题讨论】:

  • nestjs 哪个版本?
  • @raven 版本 7.0.7
  • 你能在 GitHub 上为此创建一个问题吗?我认为这是一个错误。

标签: nestjs


【解决方案1】:

最佳解决方案是基于this official comment:创建自己的formatError 或formatResponse 函数。

我刚刚使用了in this project on GitHub,效果很好!

默认情况下,需要将更改添加到 src/app.module.ts。

我的示例:

    import { GraphQLError, GraphQLFormattedError } from 'graphql';
    
    GraphQLModule.forRoot({
      autoSchemaFile: true,
      debug: false,
      formatError: (error: GraphQLError) => {
        const graphQLFormattedError: GraphQLFormattedError = {
          message: error?.extensions?.exception?.response?.message || error?.message,
        };
        return graphQLFormattedError;
      },
    }),

现在,我收到了来自 GraphQL 的格式化错误响应:

{
  "errors": [
    {
      "message": [
        "firstName must be longer than or equal to 1 characters",
        "lastName must be longer than or equal to 1 characters"
      ]
    }
  ],
  "data": null
}

【讨论】:

  • 我会添加可选链接。如error.extensions?.exception?.response?.message。 GraphQLError 中的 extensions 属性有一种可能,因此不能保证
【解决方案2】:

当您使用验证管道时,您可以更改该行为:

app.useGlobalPipes(
    new ValidationPipe({
      exceptionFactory: errors => new BadRequestException(errors), // TODO: Use graphql errors instead
      forbidUnknownValues: true,
    }),
  );
【解决方案3】:

我最终在 main.ts 中执行此操作(尽管我确信有更好的解决方案)

  app.useGlobalPipes(
    new ValidationPipe({
      exceptionFactory: (errors: ValidationError[]) => {
        const error_messages = errors.map(error =>
          Object.values(error.constraints),
        );
        return new BadRequestException(error_messages.toString());
      },
      forbidUnknownValues: false,
    }),
  );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2017-12-19
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多