自定义 GraphQL 异常

Custom GraphQL exceptions

提问人:cdelrey 提问时间:9/26/2023 更新时间:9/26/2023 访问量:47

问:

我正在尝试在 GraphQL 抛出自己的异常时创建一个自定义异常。我得到的默认 GraphQL 异常响应是(填充物除外):

{
    "errors": [
        {
            "message": "Validation error of type WrongType: argument 'inputLocals.localId' with value 'IntValue{value=7777800}' is not a valid 'Int' - Expected value to be in the Integer range but it was '7777800' @ 'getLocals'",
            "locations": [
                {
                    "line": 1,
                    "column": 25
                }
            ],
            "extensions": {
                "classification": "ValidationError"
            }
        }
    ],
    "data": null
}

所以这是我在超过 int 限制时遇到的错误,我希望它是这样的:

{
    "code": "400",
    "reason": "BAD_REQUEST",
    "message": "/localId 7777800 is not less or equal to 99999",
    "status": "",
    "referenceError": ""
}

我正在使用 Kickstart GraphQL 库,我已经检查了一些 GraphQL 错误处理程序,但它不符合预期(格式相同)。我希望在解析器中使用此查询时出现异常:

  @Override
    public OutputLocals getLocals(InputLocals inputLocals) throws CustomGraphQLError {
        int localId = inputLocals.localId();
        if (localId > 99999) {
            throw new CustomGraphQLError("fields matching the JSON above");
        }
     // Rest of code
java spring-boot 异常 错误处理 graphql

评论


答: 暂无答案