Skip to content

Commit

Permalink
Add error_trace to string representation of an Error
Browse files Browse the repository at this point in the history
If the `error_trace` payload is available, add it to the string
representation of the Error class.
  • Loading branch information
seut authored and amotl committed Oct 2, 2024
1 parent 1456de0 commit d3af228
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
5 changes: 5 additions & 0 deletions src/crate/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/crate/client/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -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 ###")

0 comments on commit d3af228

Please sign in to comment.