Make incorrect label name error more descriptive

Instead of unintuitive Value Error now we have dedicated QubesLabelNotFoundError.
This commit is contained in:
Marta Marczykowska-Górecka 2020-08-13 20:07:19 +02:00
parent 72f565a36b
commit fe583cd88e
No known key found for this signature in database
GPG Key ID: 9A752C30B26FD04B
3 changed files with 16 additions and 2 deletions

View File

@ -250,7 +250,7 @@ class QubesBase(qubesadmin.base.PropertyHolder):
def get_label(self, label): def get_label(self, label):
"""Get label as identified by index or name """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 # first search for name, verbatim
@ -264,7 +264,7 @@ class QubesBase(qubesadmin.base.PropertyHolder):
for i in self.labels.values(): for i in self.labels.values():
if i.index == int(label): if i.index == int(label):
return i return i
raise KeyError(label) raise qubesadmin.exc.QubesLabelNotFoundError(label)
@staticmethod @staticmethod
def get_vm_class(clsname): def get_vm_class(clsname):

View File

@ -138,6 +138,13 @@ class QubesTagNotFoundError(QubesException, KeyError):
return QubesException.__str__(self) 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): class StoragePoolException(QubesException):
''' A general storage exception ''' ''' A general storage exception '''

View File

@ -278,6 +278,13 @@ class TC_10_QubesBase(qubesadmin.tests.QubesTestCase):
self.assertEqual(label.name, 'red') self.assertEqual(label.name, 'red')
self.assertAllCalled() 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): def clone_setup_common_calls(self, src, dst):
# have each property type with default=no, each special-cased, # have each property type with default=no, each special-cased,
# and some with default=yes # and some with default=yes