Skip to content

Commit 983977d

Browse files
committed
Fix AttributeError on exception with field source
trying to fix below error, which happens if the handler raises an custom exception with `source` field inside ``` locations: list[SourceLocation] | None = [ > source.get_location(pos) for pos in positions ^^^^^^^^^^^^^^^^^^^ ] E AttributeError: 'str' object has no attribute 'get_location' .tox/py312/lib/python3.12/site-packages/graphql/error/graphql_error.py:161: AttributeError ```
1 parent fa8f9b5 commit 983977d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/graphql/error/located_error.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING, Collection
77

88
from ..pyutils import inspect
9+
from ..language.source import is_source
910
from .graphql_error import GraphQLError
1011

1112
if TYPE_CHECKING:
@@ -39,7 +40,9 @@ def located_error(
3940
except AttributeError:
4041
message = str(original_error)
4142
try:
42-
source = original_error.source # type: ignore
43+
source = original_error.source
44+
if not is_source(source):
45+
source = None
4346
except AttributeError:
4447
source = None
4548
try:

tests/execution/test_executor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ async def nulls_out_error_subtrees():
391391
asyncRawError
392392
asyncReturnError
393393
asyncReturnErrorWithExtensions
394+
asyncRawErrorWithSource
394395
}
395396
"""
396397
)
@@ -410,10 +411,16 @@ async def nulls_out_error_subtrees():
410411
"asyncRawError": GraphQLField(GraphQLString),
411412
"asyncReturnError": GraphQLField(GraphQLString),
412413
"asyncReturnErrorWithExtensions": GraphQLField(GraphQLString),
414+
"asyncRawErrorWithSource": GraphQLField(GraphQLString),
413415
},
414416
)
415417
)
416418

419+
class CustomException(Exception):
420+
def __init__(self, message, source):
421+
super().__init__(message)
422+
self.source = source
423+
417424
# noinspection PyPep8Naming,PyMethodMayBeStatic
418425
class Data:
419426
def syncOk(self, _info):
@@ -448,6 +455,9 @@ async def asyncRawError(self, _info):
448455
async def asyncReturnError(self, _info):
449456
return GraphQLError("Error getting asyncReturnError")
450457

458+
async def asyncRawErrorWithSource(self, _info):
459+
raise CustomException("Error getting asyncRawErrorWithSource", source="source")
460+
451461
async def asyncReturnErrorWithExtensions(self, _info):
452462
return GraphQLError(
453463
"Error getting asyncReturnErrorWithExtensions",
@@ -470,6 +480,7 @@ async def asyncReturnErrorWithExtensions(self, _info):
470480
"asyncRawError": None,
471481
"asyncReturnError": None,
472482
"asyncReturnErrorWithExtensions": None,
483+
"asyncRawErrorWithSource": None,
473484
},
474485
[
475486
{
@@ -518,6 +529,11 @@ async def asyncReturnErrorWithExtensions(self, _info):
518529
"path": ["asyncReturnErrorWithExtensions"],
519530
"extensions": {"foo": "bar"},
520531
},
532+
{
533+
"message": "Error getting asyncRawErrorWithSource",
534+
"locations": [(13, 15)],
535+
"path": ["asyncRawErrorWithSource"],
536+
},
521537
],
522538
)
523539

0 commit comments

Comments
 (0)