Skip to content

Commit f983e19

Browse files
committed
Improved error repr
1 parent 453404c commit f983e19

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

graphql/error/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44

55
class GraphQLError(Exception):
6-
__slots__ = 'message', 'nodes', 'stack', 'original_error', '_source', '_positions'
6+
__slots__ = 'message', 'nodes', 'stack', 'original_error', '_source', '_positions', '_locations'
77

8-
def __init__(self, message, nodes=None, stack=None, source=None, positions=None):
8+
def __init__(self, message, nodes=None, stack=None, source=None, positions=None, locations=None):
99
super(GraphQLError, self).__init__(message)
1010
self.message = message
1111
self.nodes = nodes
1212
self.stack = stack
1313
self._source = source
1414
self._positions = positions
15+
self._locations = locations
1516

1617
@property
1718
def source(self):
@@ -38,6 +39,8 @@ def reraise(self):
3839

3940
@property
4041
def locations(self):
41-
source = self.source
42-
if self.positions and source:
43-
return [get_location(source, pos) for pos in self.positions]
42+
if not self._locations:
43+
source = self.source
44+
if self.positions and source:
45+
self._locations = [get_location(source, pos) for pos in self.positions]
46+
return self._locations

graphql/language/location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, line, column):
99
self.column = column
1010

1111
def __repr__(self):
12-
return '<SourceLocation line={} column={}>'.format(self.line, self.column)
12+
return 'SourceLocation(line={}, column={})'.format(self.line, self.column)
1313

1414
def __eq__(self, other):
1515
return (

graphql/language/tests/test_location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
def test_repr_source_location():
55
loc = SourceLocation(10, 25)
6-
assert repr(loc) == '<SourceLocation line=10 column=25>'
6+
assert repr(loc) == 'SourceLocation(line=10, column=25)'

0 commit comments

Comments
 (0)