api: use more generic error member for exceptions

Rename the `API.exception` member to `API.error`, to make it more
generic, so it can also be used for other sort of errors in the
future. Also add a layer of additional structure with `type` and
`data` members so different types of errors apart. Currently only
`exception` is used.
Adapt the tests in test/mod/test_api.py to check for the new
structure and its content.
This commit is contained in:
Christian Kellner 2020-10-07 16:04:28 +02:00
parent 17fbe41b03
commit f5d00dd043
3 changed files with 14 additions and 6 deletions

View file

@ -144,7 +144,7 @@ class API(BaseAPI):
self._output_pipe = None
self.monitor = monitor
self.metadata = {}
self.exception = None
self.error = None
@property
def output(self):
@ -187,7 +187,10 @@ class API(BaseAPI):
sock.send({"type": "fd", "fd": 0}, fds=fds)
def _get_exception(self, message):
self.exception = message["exception"]
self.error = {
"type": "exception",
"data": message["exception"],
}
def _message(self, msg, fds, sock):
if msg["method"] == 'add-metadata':

View file

@ -93,7 +93,7 @@ class Stage:
binds=[os.fspath(tree) + ":/run/osbuild/tree"],
readonly_binds=ro_binds)
return BuildResult(self, r.returncode, r.output, api.metadata, api.exception)
return BuildResult(self, r.returncode, r.output, api.metadata, api.error)
class Assembler:
@ -152,7 +152,7 @@ class Assembler:
binds=binds,
readonly_binds=ro_binds)
return BuildResult(self, r.returncode, r.output, api.metadata, api.exception)
return BuildResult(self, r.returncode, r.output, api.metadata, api.error)
class Pipeline:

View file

@ -96,8 +96,13 @@ class TestAPI(unittest.TestCase):
p = mp.Process(target=exception, args=(path, ))
p.start()
p.join()
self.assertIsNotNone(api.exception, "Exception not set")
self.assertEqual(api.exception["value"], "osbuild test exception")
self.assertIsNotNone(api.error, "Error not set")
self.assertIn("type", api.error, "Error has no 'type' set")
self.assertEqual("exception", api.error["type"], "Not an exception")
e = api.error["data"]
for field in ("type", "value", "traceback"):
self.assertIn(field, e, f"Exception needs '{field}'")
self.assertEqual(e["value"], "osbuild test exception")
def test_metadata(self):
# Check that `api.metadata` leads to `API.metadata` being