Skip to content

Commit

Permalink
Added reraise method to GraphQLError
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 15, 2016
1 parent 5474674 commit 311b3b9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ install:
- pip install pytest==2.9.2
- pip install -e .
script:
- flake8
- py.test --cov=graphql graphql tests
after_success:
- coveralls
Expand All @@ -36,8 +35,10 @@ matrix:
after_install:
- pip install pytest-asyncio
script:
- flake8
- py.test --cov=graphql graphql tests tests_py35
- python: '2.7'
script:
- flake8
deploy:
provider: pypi
user: syrusakbary
Expand Down
7 changes: 7 additions & 0 deletions graphql/error/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import six
from ..language.location import get_location


Expand Down Expand Up @@ -29,6 +30,12 @@ def positions(self):
if any(node_positions):
return node_positions

def reraise(self):
if self.stack:
six.reraise(type(self), self, self.stack)
else:
raise self

@property
def locations(self):
source = self.source
Expand Down
Empty file added graphql/error/tests/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions graphql/error/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
import traceback

from graphql.execution import execute
from graphql.language.parser import parse
from graphql.type import (GraphQLField, GraphQLObjectType, GraphQLSchema,
GraphQLString)


def test_raise():
ast = parse('query Example { a }')

def resolver(context, *_):
raise Exception('Failed')

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

result = execute(GraphQLSchema(Type), ast)
assert str(result.errors[0]) == 'Failed'


def test_reraise():
ast = parse('query Example { a }')

def resolver(context, *_):
raise Exception('Failed')

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

result = execute(GraphQLSchema(Type), ast)
with pytest.raises(Exception) as exc_info:
result.errors[0].reraise()

extracted = traceback.extract_tb(exc_info.tb)
formatted_tb = [row[2:] for row in extracted]
if formatted_tb[2][0] == 'reraise':
formatted_tb[2:] = formatted_tb[3:]

assert formatted_tb == [
('test_reraise', 'result.errors[0].reraise()'),
('reraise', 'six.reraise(type(self), self, self.stack)'),
# ('reraise', 'raise value.with_traceback(tb)'),
('resolve_or_error', 'return executor.execute(resolve_fn, source, args, context, info)'),
('execute', 'return fn(*args, **kwargs)'), ('resolver', "raise Exception('Failed')")
]
assert str(exc_info.value) == 'Failed'
1 change: 1 addition & 0 deletions graphql/language/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def read_token(source, from_position):
source, position,
u'Unexpected character {}.'.format(print_char_code(code)))


ignored_whitespace_characters = frozenset([
# BOM
0xFEFF,
Expand Down
1 change: 1 addition & 0 deletions graphql/type/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, name, description=None, args=None, locations=None):
_arg.type)
self.args = args or OrderedDict()


"""Used to conditionally include fields or fragments."""
GraphQLIncludeDirective = GraphQLDirective(
name='include',
Expand Down

0 comments on commit 311b3b9

Please sign in to comment.