Skip to content

Error handling more closely as graphql-js #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions graphql/core/error/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .graphql_error import GraphQLError, format_error

__all__ = ['GraphQLError', 'format_error']
14 changes: 5 additions & 9 deletions graphql/core/error.py → graphql/core/error/graphql_error.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from .language.location import get_location
from ..language.location import get_location


class Error(Exception):
pass
class GraphQLError(Exception):
__slots__ = 'message', 'nodes', '_source', '_positions', 'original_error'


class GraphQLError(Error):
__slots__ = 'message', 'nodes', 'stack', '_source', '_positions'

def __init__(self, message, nodes=None, stack=None, source=None, positions=None):
def __init__(self, message, nodes=None, source=None, positions=None, original_error=None):
super(GraphQLError, self).__init__(message)
self.message = message or 'An unknown error occurred.'
self.nodes = nodes
self.stack = stack or message
self._source = source
self._positions = positions
self.original_error = original_error

@property
def source(self):
Expand Down
20 changes: 18 additions & 2 deletions graphql/core/execution/executor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import collections
import functools
import sys

from six import reraise

from ..error import GraphQLError
from ..language import ast
Expand All @@ -17,6 +20,14 @@
get_operation_root_type)


def _raise_with_original_stack_trace(original_error, new_error_type, new_error):
try:
raise original_error
except:
_, __, original_traceback = sys.exc_info()
reraise(new_error_type, new_error, original_traceback)


class Executor(object):

def __init__(self, execution_middlewares=None, default_resolver=default_resolve_fn, map_type=dict):
Expand Down Expand Up @@ -227,11 +238,16 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
info,
resolved
),
lambda error: GraphQLError(error.value and str(error.value), field_asts, error)
lambda error: _raise_with_original_stack_trace(
error,
GraphQLError,
GraphQLError(error.value and str(error.value), field_asts, original_error=error)
)
)

if isinstance(result, Exception):
raise GraphQLError(str(result), field_asts, result)
_raise_with_original_stack_trace(
result, GraphQLError, GraphQLError(result.message or str(result), field_asts, original_error=result))

if isinstance(return_type, GraphQLNonNull):
completed = self.complete_value(
Expand Down
2 changes: 1 addition & 1 deletion graphql/core/validation/rules/unique_argument_names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ...error import GraphQLError
from ...error.graphql_error import GraphQLError
from .base import ValidationRule


Expand Down
2 changes: 1 addition & 1 deletion graphql/core/validation/rules/unique_operation_names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ...error import GraphQLError
from ...error.graphql_error import GraphQLError
from .base import ValidationRule


Expand Down
2 changes: 1 addition & 1 deletion graphql/core/validation/rules/variables_are_input_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ...error import GraphQLError
from ...error.graphql_error import GraphQLError
from ...language.printer import print_ast
from ...type.definition import is_input_type
from ...utils.type_from_ast import type_from_ast
Expand Down
2 changes: 1 addition & 1 deletion tests/core_execution/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pytest import raises

from graphql.core.error import GraphQLError
from graphql.core.error.graphql_error import GraphQLError
from graphql.core.execution import Executor, execute
from graphql.core.execution.middlewares.sync import \
SynchronousExecutionMiddleware
Expand Down
2 changes: 1 addition & 1 deletion tests/core_execution/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import raises

from graphql.core.error import GraphQLError, format_error
from graphql.core.error.graphql_error import GraphQLError, format_error
from graphql.core.execution import execute
from graphql.core.language.parser import parse
from graphql.core.type import (GraphQLArgument, GraphQLField,
Expand Down
1 change: 1 addition & 0 deletions tests/core_utils/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_cannot_be_used_for_execution():
clientQuery = parse('{ newField }')

result = execute(extended_schema, object(), clientQuery)
print(result)
assert result.data['newField'] is None
assert str(result.errors[0]
) == 'Client Schema cannot be used for execution.'
Expand Down