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:
Christian Kellner 2020-10-07 17:14:03 +02:00
parent 7a112c27cb
commit aaa51e22a6
2 changed files with 5 additions and 1 deletions

View file

@ -210,12 +210,15 @@ def exception(e, path="/run/osbuild/api/osbuild"):
"""Send exception to osbuild"""
traceback.print_exception(type(e), e, e.__traceback__, file=sys.stderr)
with jsoncomm.Socket.new_client(path) as client:
with io.StringIO() as out:
traceback.print_tb(e.__traceback__, file=out)
stacktrace = out.getvalue()
msg = {
"method": "exception",
"exception": {
"type": type(e).__name__,
"value": str(e),
"traceback": str(e.__traceback__)
"traceback": stacktrace
}
}
client.send(msg)

View file

@ -103,6 +103,7 @@ class TestAPI(unittest.TestCase):
self.assertIn(field, e, f"Exception needs '{field}'")
self.assertEqual(e["value"], "osbuild test exception")
self.assertEqual(e["type"], "ValueError")
self.assertIn("exception", e["traceback"])
def test_metadata(self):
# Check that `api.metadata` leads to `API.metadata` being