Add error code BAD_USER_INPUT to validation exception
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?
That documentation is specific to Apollo and their implementation. We can however add something like that.
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?
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
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?
Yes, what do you get now ?
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
}
}
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?
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
@t1 Have you looked into this? Or could you link me your reproducer?