From a5450f0298ddac8b89312d423e3ff7f8fc41dd94 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Wed, 8 Jun 2022 11:52:04 +0000 Subject: [PATCH] Cast error message to string to handle proxy objects --- src/graphql/error/located_error.py | 2 +- tests/error/test_located_error.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/graphql/error/located_error.py b/src/graphql/error/located_error.py index 0f81deae..60bc63c9 100644 --- a/src/graphql/error/located_error.py +++ b/src/graphql/error/located_error.py @@ -29,7 +29,7 @@ def located_error( return original_error try: # noinspection PyUnresolvedReferences - message = original_error.message # type: ignore + message = str(original_error.message) # type: ignore except AttributeError: message = str(original_error) try: diff --git a/tests/error/test_located_error.py b/tests/error/test_located_error.py index cfb252e6..e8a72793 100644 --- a/tests/error/test_located_error.py +++ b/tests/error/test_located_error.py @@ -2,6 +2,8 @@ from graphql.error import GraphQLError, located_error +from ..utils import dedent + def describe_located_error(): def throws_without_an_original_error(): @@ -23,3 +25,24 @@ def does_not_pass_through_elasticsearch_like_errors(): e = Exception("I am from elasticsearch") cast(Any, e).path = "/something/feed/_search" assert located_error(e, [], []) is not e + + def handles_proxy_error_messages(): + class ProxyString: + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value + + class MyError(Exception): + def __init__(self): + self.message = ProxyString("Example error") + super().__init__() + + error = located_error(MyError(), [], []) + + assert str(error) == dedent( + """ + Example error + """ + )