From 1e4730bc68cd4da701881bde01d0dd0ec350a42a Mon Sep 17 00:00:00 2001 From: Sebastian Utz Date: Wed, 2 Oct 2024 18:11:25 +0200 Subject: [PATCH] Add `error_trace` to string representation of an Error If the `error_trace` payload is available, add it to the string representation of the Error class. --- CHANGES.txt | 2 ++ src/crate/client/exceptions.py | 5 +++++ src/crate/client/test_exceptions.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 src/crate/client/test_exceptions.py diff --git a/CHANGES.txt b/CHANGES.txt index e58819ce..4a0f0a48 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -10,6 +10,8 @@ Unreleased about necessary migration steps. - Configured DB API interface attribute ``threadsafety = 1``, which signals "Threads may share the module, but not connections." +- Added ``error_trace`` to string representation of an Error to relay + server stacktraces into exception messages. .. _Migrate from crate.client to sqlalchemy-cratedb: https://cratedb.com/docs/sqlalchemy-cratedb/migrate-from-crate-client.html .. _sqlalchemy-cratedb: https://pypi.org/project/sqlalchemy-cratedb/ diff --git a/src/crate/client/exceptions.py b/src/crate/client/exceptions.py index 71bf5d8d..175cb30c 100644 --- a/src/crate/client/exceptions.py +++ b/src/crate/client/exceptions.py @@ -30,6 +30,11 @@ def __init__(self, msg=None, error_trace=None): super(Error, self).__init__(msg) self.error_trace = error_trace + def __str__(self): + if self.error_trace is None: + return super().__str__() + return "\n".join([super().__str__(), str(self.error_trace)]) + class Warning(Exception): pass diff --git a/src/crate/client/test_exceptions.py b/src/crate/client/test_exceptions.py new file mode 100644 index 00000000..23f5ad68 --- /dev/null +++ b/src/crate/client/test_exceptions.py @@ -0,0 +1,14 @@ +import unittest + +from crate.client import Error + + +class ErrorTestCase(unittest.TestCase): + + def test_error_with_msg(self): + err = Error("foo") + self.assertEqual(str(err), "foo") + + def test_error_with_error_trace(self): + err = Error("foo", error_trace="### TRACE ###") + self.assertEqual(str(err), "foo\n### TRACE ###")