Skip to content

Commit 31f18ce

Browse files
author
chinskiy
committed
fix UnicodeDecodeError in format_error
1 parent 9202021 commit 31f18ce

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

graphql/error/format_error.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from six import text_type
2-
31
from .base import GraphQLError
42

53
# Necessary for static type checking
@@ -9,7 +7,11 @@
97

108
def format_error(error):
119
# type: (Exception) -> Dict[str, Any]
12-
formatted_error = {"message": text_type(error)} # type: Dict[str, Any]
10+
try:
11+
message = str(error)
12+
except UnicodeEncodeError:
13+
message = error.message.encode("utf-8") # type: ignore
14+
formatted_error = {"message": message} # type: Dict[str, Any]
1315
if isinstance(error, GraphQLError):
1416
if error.locations is not None:
1517
formatted_error["locations"] = [
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# coding: utf-8
2+
from graphql.error import GraphQLError, format_error
3+
4+
5+
def test_unicode_format_error():
6+
# type: () -> None
7+
e = GraphQLError("UNIÇODÉ!")
8+
assert isinstance(format_error(e), dict)
9+
10+
e = GraphQLError("\xd0\xbe\xd1\x88\xd0\xb8\xd0\xb1\xd0\xba\xd0\xb0")
11+
assert isinstance(format_error(e), dict)

0 commit comments

Comments
 (0)