-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathbase.py
83 lines (74 loc) · 2.3 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import six
from ..language.location import get_location
# Necessary for static type checking
if False: # flake8: noqa
from ..language.source import Source
from ..language.location import SourceLocation
from types import TracebackType
from typing import Optional, List, Any, Union
class GraphQLError(Exception):
__slots__ = (
"message",
"nodes",
"stack",
"original_error",
"_source",
"_positions",
"_locations",
"path",
)
def __init__(
self,
message, # type: str
nodes=None, # type: Any
stack=None, # type: Optional[TracebackType]
source=None, # type: Optional[Any]
positions=None, # type: Optional[Any]
locations=None, # type: Optional[Any]
path=None, # type: Union[List[Union[int, str]], List[str], None]
):
# type: (...) -> None
super(GraphQLError, self).__init__(message)
self.message = message
self.nodes = nodes
self.stack = stack
self._source = source
self._positions = positions
self._locations = locations
self.path = path
return None
@property
def source(self):
# type: () -> Optional[Source]
if self._source:
return self._source
if self.nodes:
node = self.nodes[0]
return node and node.loc and node.loc.source
return None
@property
def positions(self):
# type: () -> Optional[List[int]]
if self._positions:
return self._positions
if self.nodes is not None:
node_positions = [node.loc and node.loc.start for node in self.nodes]
if any(node_positions):
return node_positions
return None
def reraise(self):
# type: () -> None
if self.stack:
six.reraise(type(self), self, self.stack)
else:
raise self
@property
def locations(self):
# type: () -> Optional[List[SourceLocation]]
if not self._locations:
source = self.source
if self.positions and source:
self._locations = [get_location(source, pos) for pos in self.positions]
return self._locations
class HandledGraphQLError(GraphQLError):
pass