Skip to content

fix(graphene)handle unicode exception message #92

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphql/error/located_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class GraphQLLocatedError(GraphQLError):

def __init__(self, nodes, original_error=None):
if original_error:
message = str(original_error)
try:
message = str(original_error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if instead of message = str(original_error) we do message= original_error.message always?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in Python 3.X Exception doesn't have .message attribute

except UnicodeEncodeError:
message = original_error.message.encode('utf-8')
else:
message = 'An unknown error occurred.'

Expand Down
23 changes: 23 additions & 0 deletions graphql/execution/tests/test_located_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8

from graphql import GraphQLField
from graphql import GraphQLObjectType
from graphql import GraphQLSchema
from graphql import GraphQLString
from graphql import execute
from graphql import parse
from graphql.error import GraphQLLocatedError


def test_unicode_error_message():
ast = parse('query Example { unicode }')

def resolver(context, *_):
raise Exception(u'UNIÇODÉ!')

Type = GraphQLObjectType('Type', {
'unicode': GraphQLField(GraphQLString, resolver=resolver),
})

result = execute(GraphQLSchema(Type), ast)
assert isinstance(result.errors[0], GraphQLLocatedError)