File tree 2 files changed +22
-3
lines changed
2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 1
- from six import text_type
2
-
3
1
from .base import GraphQLError
4
2
5
3
# Necessary for static type checking
9
7
10
8
def format_error (error ):
11
9
# 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]
13
16
if isinstance (error , GraphQLError ):
14
17
if error .locations is not None :
15
18
formatted_error ["locations" ] = [
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments