api: properly serialize the exception's traceback
Use `traceback.print_tb()` to serialize the exceptions' backtrace. The previously used expression `str(e.__traceback__)` will just give `<traceback object at 0x…>`, which is not very helpful. Add a test to check that the method name that raises the exception, also called `exception`, is in the traceback.
This commit is contained in:
parent
7a112c27cb
commit
aaa51e22a6
2 changed files with 5 additions and 1 deletions
|
|
@ -210,12 +210,15 @@ def exception(e, path="/run/osbuild/api/osbuild"):
|
||||||
"""Send exception to osbuild"""
|
"""Send exception to osbuild"""
|
||||||
traceback.print_exception(type(e), e, e.__traceback__, file=sys.stderr)
|
traceback.print_exception(type(e), e, e.__traceback__, file=sys.stderr)
|
||||||
with jsoncomm.Socket.new_client(path) as client:
|
with jsoncomm.Socket.new_client(path) as client:
|
||||||
|
with io.StringIO() as out:
|
||||||
|
traceback.print_tb(e.__traceback__, file=out)
|
||||||
|
stacktrace = out.getvalue()
|
||||||
msg = {
|
msg = {
|
||||||
"method": "exception",
|
"method": "exception",
|
||||||
"exception": {
|
"exception": {
|
||||||
"type": type(e).__name__,
|
"type": type(e).__name__,
|
||||||
"value": str(e),
|
"value": str(e),
|
||||||
"traceback": str(e.__traceback__)
|
"traceback": stacktrace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client.send(msg)
|
client.send(msg)
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,7 @@ class TestAPI(unittest.TestCase):
|
||||||
self.assertIn(field, e, f"Exception needs '{field}'")
|
self.assertIn(field, e, f"Exception needs '{field}'")
|
||||||
self.assertEqual(e["value"], "osbuild test exception")
|
self.assertEqual(e["value"], "osbuild test exception")
|
||||||
self.assertEqual(e["type"], "ValueError")
|
self.assertEqual(e["type"], "ValueError")
|
||||||
|
self.assertIn("exception", e["traceback"])
|
||||||
|
|
||||||
def test_metadata(self):
|
def test_metadata(self):
|
||||||
# Check that `api.metadata` leads to `API.metadata` being
|
# Check that `api.metadata` leads to `API.metadata` being
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue