From fe583cd88e56e3aceafa415d9f7fababc03c4e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Thu, 13 Aug 2020 20:07:19 +0200 Subject: [PATCH] Make incorrect label name error more descriptive Instead of unintuitive Value Error now we have dedicated QubesLabelNotFoundError. --- qubesadmin/app.py | 4 ++-- qubesadmin/exc.py | 7 +++++++ qubesadmin/tests/app.py | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/qubesadmin/app.py b/qubesadmin/app.py index d25ccaa..7de88f9 100644 --- a/qubesadmin/app.py +++ b/qubesadmin/app.py @@ -250,7 +250,7 @@ class QubesBase(qubesadmin.base.PropertyHolder): def get_label(self, label): """Get label as identified by index or name - :throws KeyError: when label is not found + :throws QubesLabelNotFoundError: when label is not found """ # first search for name, verbatim @@ -264,7 +264,7 @@ class QubesBase(qubesadmin.base.PropertyHolder): for i in self.labels.values(): if i.index == int(label): return i - raise KeyError(label) + raise qubesadmin.exc.QubesLabelNotFoundError(label) @staticmethod def get_vm_class(clsname): diff --git a/qubesadmin/exc.py b/qubesadmin/exc.py index 65b6dd9..f55ceba 100644 --- a/qubesadmin/exc.py +++ b/qubesadmin/exc.py @@ -138,6 +138,13 @@ class QubesTagNotFoundError(QubesException, KeyError): return QubesException.__str__(self) +class QubesLabelNotFoundError(QubesException, KeyError): + """Label does not exists""" + def __str__(self): + # KeyError overrides __str__ method + return QubesException.__str__(self) + + class StoragePoolException(QubesException): ''' A general storage exception ''' diff --git a/qubesadmin/tests/app.py b/qubesadmin/tests/app.py index 5a57015..179b05b 100644 --- a/qubesadmin/tests/app.py +++ b/qubesadmin/tests/app.py @@ -278,6 +278,13 @@ class TC_10_QubesBase(qubesadmin.tests.QubesTestCase): self.assertEqual(label.name, 'red') self.assertAllCalled() + def test_021_get_nonexistant_label(self): + self.app.expected_calls[('dom0', 'admin.label.List', None, None)] = \ + b'0\x00red\nblue\n' + with self.assertRaises(qubesadmin.exc.QubesLabelNotFoundError): + self.app.get_label('green') + self.assertAllCalled() + def clone_setup_common_calls(self, src, dst): # have each property type with default=no, each special-cased, # and some with default=yes