Skip to content

Commit 311b3b9

Browse files
committed
Added reraise method to GraphQLError
1 parent 5474674 commit 311b3b9

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ install:
2626
- pip install pytest==2.9.2
2727
- pip install -e .
2828
script:
29-
- flake8
3029
- py.test --cov=graphql graphql tests
3130
after_success:
3231
- coveralls
@@ -36,8 +35,10 @@ matrix:
3635
after_install:
3736
- pip install pytest-asyncio
3837
script:
39-
- flake8
4038
- py.test --cov=graphql graphql tests tests_py35
39+
- python: '2.7'
40+
script:
41+
- flake8
4142
deploy:
4243
provider: pypi
4344
user: syrusakbary

graphql/error/base.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import six
12
from ..language.location import get_location
23

34

@@ -29,6 +30,12 @@ def positions(self):
2930
if any(node_positions):
3031
return node_positions
3132

33+
def reraise(self):
34+
if self.stack:
35+
six.reraise(type(self), self, self.stack)
36+
else:
37+
raise self
38+
3239
@property
3340
def locations(self):
3441
source = self.source

graphql/error/tests/__init__.py

Whitespace-only changes.

graphql/error/tests/test_base.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
import traceback
3+
4+
from graphql.execution import execute
5+
from graphql.language.parser import parse
6+
from graphql.type import (GraphQLField, GraphQLObjectType, GraphQLSchema,
7+
GraphQLString)
8+
9+
10+
def test_raise():
11+
ast = parse('query Example { a }')
12+
13+
def resolver(context, *_):
14+
raise Exception('Failed')
15+
16+
Type = GraphQLObjectType('Type', {
17+
'a': GraphQLField(GraphQLString, resolver=resolver),
18+
})
19+
20+
result = execute(GraphQLSchema(Type), ast)
21+
assert str(result.errors[0]) == 'Failed'
22+
23+
24+
def test_reraise():
25+
ast = parse('query Example { a }')
26+
27+
def resolver(context, *_):
28+
raise Exception('Failed')
29+
30+
Type = GraphQLObjectType('Type', {
31+
'a': GraphQLField(GraphQLString, resolver=resolver),
32+
})
33+
34+
result = execute(GraphQLSchema(Type), ast)
35+
with pytest.raises(Exception) as exc_info:
36+
result.errors[0].reraise()
37+
38+
extracted = traceback.extract_tb(exc_info.tb)
39+
formatted_tb = [row[2:] for row in extracted]
40+
if formatted_tb[2][0] == 'reraise':
41+
formatted_tb[2:] = formatted_tb[3:]
42+
43+
assert formatted_tb == [
44+
('test_reraise', 'result.errors[0].reraise()'),
45+
('reraise', 'six.reraise(type(self), self, self.stack)'),
46+
# ('reraise', 'raise value.with_traceback(tb)'),
47+
('resolve_or_error', 'return executor.execute(resolve_fn, source, args, context, info)'),
48+
('execute', 'return fn(*args, **kwargs)'), ('resolver', "raise Exception('Failed')")
49+
]
50+
assert str(exc_info.value) == 'Failed'

graphql/language/lexer.py

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def read_token(source, from_position):
183183
source, position,
184184
u'Unexpected character {}.'.format(print_char_code(code)))
185185

186+
186187
ignored_whitespace_characters = frozenset([
187188
# BOM
188189
0xFEFF,

graphql/type/directives.py

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, name, description=None, args=None, locations=None):
6868
_arg.type)
6969
self.args = args or OrderedDict()
7070

71+
7172
"""Used to conditionally include fields or fragments."""
7273
GraphQLIncludeDirective = GraphQLDirective(
7374
name='include',

0 commit comments

Comments
 (0)