Skip to content

Commit 87b6156

Browse files
chinskiydan98765
chinskiy
authored andcommitted
fix UnicodeDecodeError in format_error #216 (#223)
1 parent 1760eaf commit 87b6156

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

graphql/error/format_error.py

+6-3
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,12 @@
97

108
def format_error(error):
119
# type: (Exception) -> Dict[str, Any]
12-
formatted_error = {"message": text_type(error)} # type: Dict[str, Any]
10+
# Protect against UnicodeEncodeError when run in py2 (#216)
11+
try:
12+
message = str(error)
13+
except UnicodeEncodeError:
14+
message = error.message.encode("utf-8") # type: ignore
15+
formatted_error = {"message": message} # type: Dict[str, Any]
1316
if isinstance(error, GraphQLError):
1417
if error.locations is not None:
1518
formatted_error["locations"] = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# coding: utf-8
2+
import pytest
3+
4+
from graphql.error import GraphQLError, format_error
5+
6+
7+
@pytest.mark.parametrize(
8+
"error",
9+
[
10+
GraphQLError("UNIÇODÉ!"),
11+
GraphQLError("\xd0\xbe\xd1\x88\xd0\xb8\xd0\xb1\xd0\xba\xd0\xb0"),
12+
],
13+
)
14+
def test_unicode_format_error(error):
15+
# type: (GraphQLError) -> None
16+
assert isinstance(format_error(error), dict)

0 commit comments

Comments
 (0)