Skip to content

Commit d3af228

Browse files
seutamotl
authored andcommitted
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.
1 parent 1456de0 commit d3af228

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Unreleased
1010
about necessary migration steps.
1111
- Configured DB API interface attribute ``threadsafety = 1``, which signals
1212
"Threads may share the module, but not connections."
13+
- Added ``error_trace`` to string representation of an Error to relay
14+
server stacktraces into exception messages.
1315

1416
.. _Migrate from crate.client to sqlalchemy-cratedb: https://cratedb.com/docs/sqlalchemy-cratedb/migrate-from-crate-client.html
1517
.. _sqlalchemy-cratedb: https://pypi.org/project/sqlalchemy-cratedb/

src/crate/client/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def __init__(self, msg=None, error_trace=None):
3030
super(Error, self).__init__(msg)
3131
self.error_trace = error_trace
3232

33+
def __str__(self):
34+
if self.error_trace is None:
35+
return super().__str__()
36+
return "\n".join([super().__str__(), str(self.error_trace)])
37+
3338

3439
class Warning(Exception):
3540
pass

src/crate/client/test_exceptions.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
3+
from crate.client import Error
4+
5+
6+
class ErrorTestCase(unittest.TestCase):
7+
8+
def test_error_with_msg(self):
9+
err = Error("foo")
10+
self.assertEqual(str(err), "foo")
11+
12+
def test_error_with_error_trace(self):
13+
err = Error("foo", error_trace="### TRACE ###")
14+
self.assertEqual(str(err), "foo\n### TRACE ###")

0 commit comments

Comments
 (0)