From 5615510da5d0cdda0f5d070e5dbaa347d36688c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 11 Mar 2017 01:41:58 +0100 Subject: [PATCH] Add specific exception for qubesd communication error --- qubesmgmt/base.py | 8 +++++++- qubesmgmt/exc.py | 10 ++++++++++ qubesmgmt/tests/errors.py | 9 +++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/qubesmgmt/base.py b/qubesmgmt/base.py index 8dfe563..784a5fb 100644 --- a/qubesmgmt/base.py +++ b/qubesmgmt/base.py @@ -69,6 +69,11 @@ class PropertyHolder(object): In case of success, return actual data. In case of error, raise appropriate exception. ''' + + if len(response_data) == 0: + raise qubesmgmt.exc.QubesDaemonNoResponseError( + 'Got empty response from qubesd') + if response_data[0:2] == b'\x30\x00': return response_data[2:] elif response_data[0:2] == b'\x32\x00': @@ -89,7 +94,8 @@ class PropertyHolder(object): # TODO: handle traceback if given raise exc_class(format_string, *args) else: - raise qubesmgmt.exc.QubesException('Invalid response format') + raise qubesmgmt.exc.QubesDaemonCommunicationError( + 'Invalid response format') def property_list(self): ''' diff --git a/qubesmgmt/exc.py b/qubesmgmt/exc.py index efd1101..7de410b 100644 --- a/qubesmgmt/exc.py +++ b/qubesmgmt/exc.py @@ -101,3 +101,13 @@ class BackupCancelledError(QubesException): class QubesMemoryError(QubesException, MemoryError): '''Cannot start domain, because not enough memory is available''' + + +class QubesDaemonCommunicationError(QubesException, IOError): + '''Error while communicating with qubesd, may mean insufficient + permissions, as well''' + + +# pylint: disable=too-many-ancestors +class QubesDaemonNoResponseError(QubesDaemonCommunicationError): + '''Got empty response from qubesd''' diff --git a/qubesmgmt/tests/errors.py b/qubesmgmt/tests/errors.py index b72da65..aed5828 100644 --- a/qubesmgmt/tests/errors.py +++ b/qubesmgmt/tests/errors.py @@ -51,8 +51,9 @@ class TC_00_Errors(qubesmgmt.tests.QubesTestCase): self.assertEqual(str(context.exception), 'An error occurred: 1, 2') def test_010_empty(self): - self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = b''\ - # FIXME: change to appropriate exception when defined - with self.assertRaises(qubesmgmt.exc.QubesException) as context: + self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = b'' + with self.assertRaises(qubesmgmt.exc.QubesDaemonNoResponseError) \ + as context: vms = list(self.app.domains) - self.assertEqual(str(context.exception), 'Invalid response format') + self.assertEqual(str(context.exception), + 'Got empty response from qubesd')