I am trying to throw a custom exception in graphql but was only able to change extensions of the GraphQlError, and I want to change the error message too. Have a look,
public class SomeException extends RuntimeException implements GraphQLError {
List<String> someErrors;
String errorMessage;
public SomeException(String message, List<String> someErrors) {
this.errorMessage = message;
this.someErrors = someErrors;
}
@Override
public String getMessage() {
return this.errorMessage;
}
@Override
public ErrorClassification getErrorType() {
return null;
}
@Override
public Map<String, Object> getExtensions() {
return Collections.singletonMap("someErrors",someErrors);
}
@Override
public List<SourceLocation> getLocations() {
return null;
}
}
So, my getMessage() function actually not overridden as it still shows this...
{
"errors": [
{
"message": "System error",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"something"
],
"extensions": {
"someErrors": [
"...",
"..."
],
"classification": "DataFetchingException"
}
}
],
"data": {
"saveInstance": null
}
}
this is Postman response when I was triggering the exception. So, I want a custom message to be inside "message".
If you are using Runtime exception you need to enable it explicitly and also tell what fields you want to show. See
And