smallrye-graphql icon indicating copy to clipboard operation
smallrye-graphql copied to clipboard

Add error code BAD_USER_INPUT to validation exception

Open robp94 opened this issue 4 years ago • 9 comments

If I use the hibernate validator to validate the input of a query or mutation, I get a response like this if it is invalid:

{
  "errors": [
    {
      "message": "validation failed: test.test.t darf nicht leer sein",
      "locations": [
        {
          "line": 2,
          "column": 3
        },
        {
          "line": 2,
          "column": 8
        }
      ],
      "path": [
        "test",
        "test"
      ],
      "extensions": {
        "violation.propertyPath": [
          "test",
          "test",
          "t"
        ],
        "violation.invalidValue": "",
        "violation.constraint": {
          "groups": [],
          "message": "{javax.validation.constraints.NotBlank.message}",
          "payload": []
        },
        "violation.message": "darf nicht leer sein",
        "classification": "ValidationError"
      }
    }
  ],
  "data": {
    "test": null
  }
}

If I understand the graphql documentation here correctly, there should be a specific error code for this https://www.apollographql.com/docs/apollo-server/data/errors/#graphql_validation_failed

BAD_USER_INPUT "The GraphQL operation includes an invalid value for a field argument."

Could we add this for the validation error?

robp94 avatar Oct 20 '21 14:10 robp94

That documentation is specific to Apollo and their implementation. We can however add something like that.

phillip-kruger avatar Oct 20 '21 14:10 phillip-kruger

I thought we'd already derive a code extension from the exception name, so this would probably be validation-error, but it's not in the response shown here. There is a config that controls which fields are provided; maybe the code is disabled? And if that's true, is BAD_USER_INPUT much better than validation-error?

t1 avatar Oct 20 '21 14:10 t1

Yes you have to switch on those fields (like code). See https://quarkus.io/guides/smallrye-graphql#quarkus-smallrye-graphql_quarkus.smallrye-graphql.error-extension-fields

phillip-kruger avatar Oct 20 '21 14:10 phillip-kruger

Hm since the quarkus properties do not work at the moment i use this config at the moment: smallrye.graphql.errorExtensionFields=exception,classification,code,description,validationErrorType,queryPath

So there should be the code validation-error?

robp94 avatar Oct 20 '21 14:10 robp94

Yes, what do you get now ?

phillip-kruger avatar Oct 20 '21 14:10 phillip-kruger

The one I posted above. With a custom exception where I use the annotation, I get the error code:

{
  "errors": [
    {
      "message": "Test",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "test"
      ],
      "extensions": {
        "exception": "TestException",
        "classification": "DataFetchingException",
        "code": "BAD_USER_INPUT"
      }
    }
  ],
  "data": {
    "test": null
  }
}

robp94 avatar Oct 20 '21 15:10 robp94

I wrote a reproducer and got:

{
    "data": {
        "sayHello": null
    },
    "errors": [
        {
            "extensions": {
                "classification": "DataFetchingException",
                "code": "constraint-violation",
                "exception": "javax.validation.ConstraintViolationException"
            },
            "locations": [
                {
                    "column": 2,
                    "line": 1
                }
            ],
            "message": "System error",
            "path": [
                "sayHello"
            ]
        }
    ]
}

@robp94: Can you share your reproducer?

t1 avatar Oct 22 '21 09:10 t1

code-with-quarkus-validation.zip

{
  "errors": [
    {
      "message": "validation failed: query.testObject.test darf nicht leer sein",
      "locations": [
        {
          "line": 1,
          "column": 9
        },
        {
          "line": 1,
          "column": 15
        }
      ],
      "path": [
        "query",
        "testObject"
      ],
      "extensions": {
        "violation.propertyPath": [
          "query",
          "testObject",
          "test"
        ],
        "violation.invalidValue": "",
        "violation.constraint": {
          "groups": [],
          "message": "{javax.validation.constraints.NotBlank.message}",
          "payload": []
        },
        "violation.message": "darf nicht leer sein",
        "classification": "ValidationError"
      }
    }
  ],
  "data": {
    "query": null
  }
}
With config:
smallrye.graphql.printDataFetcherException=true
smallrye.graphql.errorExtensionFields=exception,classification,code,description,validationErrorType,queryPath

robp94 avatar Oct 22 '21 10:10 robp94

@t1 Have you looked into this? Or could you link me your reproducer?

robp94 avatar Nov 02 '21 12:11 robp94