-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flask's app.errorhandler decorator dosen't work for GraphQLLocatedError #49
Comments
I just encountered this issue myself and looked into it. (My use case: I have a field with two mutually exclusive arguments, and need to return 400 when both are present. Isn't really possible on the schema level, and hence needs to be handled in a resolver.) The problem apparently is deep down in graphql-core. See I'm not sure if one can force an def format_execution_result(
execution_result, # type: Optional[ExecutionResult]
format_error, # type: Optional[Callable[[Exception], Dict]]
):
# type: (...) -> GraphQLResponse
status_code = 200
if execution_result:
if execution_result.invalid:
status_code = 400
response = execution_result.to_dict(format_error=format_error)
else:
response = None
return GraphQLResponse(response, status_code) Note that you can monkeypatch this function to check for any custom exception, and even deny import graphql_server
from graphql_server import GraphQLResponse
class BadRequest(Exception):
pass
class Unauthorized(Exception):
pass
def format_execution_result(
execution_result, # type: Optional[ExecutionResult]
format_error, # type: Optional[Callable[[Exception], Dict]]
):
# type: (...) -> GraphQLResponse
status_code = 200
if execution_result:
if execution_result.invalid:
status_code = 400
for e in execution_result.errors:
if isinstance(e, BadRequest):
status_code = 400
if isinstance(e, Unauthorized):
status_code = 401
# Deny data entirely on 4XX if you want to.
if status_code != 200:
execution_result.invalid = True
response = execution_result.to_dict(format_error=format_error)
else:
response = None
return GraphQLResponse(response, status_code)
# Monkeypatch
graphql_server.format_execution_result = format_execution_result Personally I've decided to not bother with the status code after all... But maybe my investigation could help someone. |
I am making an extension library flask-graphql-auth. and error handler by using flask's app.errorhandler is being planed for the 1.0 release. I checked that if my library caused an exception like JWTDecodeError, an GraphQLLocatedError occurred. However, the flask can not handle the error by app.errorhandler. Is this a bug?
I wrote this code. but stacktrace appears :(
The text was updated successfully, but these errors were encountered: