diff --git a/src/graphql/error/located_error.py b/src/graphql/error/located_error.py index 31e423bc..dc62c21c 100644 --- a/src/graphql/error/located_error.py +++ b/src/graphql/error/located_error.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Collection from ..pyutils import inspect +from ..language.source import is_source, Source from .graphql_error import GraphQLError if TYPE_CHECKING: @@ -39,7 +40,9 @@ def located_error( except AttributeError: message = str(original_error) try: - source = original_error.source # type: ignore + source = original_error.source + if not is_source(source): + source = Source(source) if isinstance(source, str) else None except AttributeError: source = None try: diff --git a/tests/error/test_located_error.py b/tests/error/test_located_error.py index f22f6fd4..08afd4ee 100644 --- a/tests/error/test_located_error.py +++ b/tests/error/test_located_error.py @@ -35,3 +35,11 @@ def __init__(self): super().__init__() assert str(located_error(LazyError())) == "lazy" + + def handles_error_with_str_source(): + class CustomException(Exception): + def __init__(self, message, source): + super().__init__(message) + self.source = source + e = located_error(CustomException("msg", "source")) + assert e.source and e.source.get_location(0)