qubes/devices: use more detailed exceptions than just KeyError

Especially inherit from QubesException, so tools will treat this as
properly handled error.

QubesOS/qubes-issues#2257
This commit is contained in:
Marek Marczykowski-Górecki 2016-09-03 02:13:02 +02:00
parent e5d6c4e078
commit e1de82ea53
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -50,6 +50,15 @@ Such extension should provide:
import qubes.utils
class DeviceNotAttached(qubes.exc.QubesException, KeyError):
'''Trying to detach not attached device'''
pass
class DeviceAlreadyAttached(qubes.exc.QubesException, KeyError):
'''Trying to attach already attached device'''
pass
class DeviceCollection(object):
'''Bag for devices.
@ -120,7 +129,7 @@ class DeviceCollection(object):
'''
if device in self.attached():
raise KeyError(
raise DeviceAlreadyAttached(
'device {!r} of class {} already attached to {!r}'.format(
device, self._class, self._vm))
self._vm.fire_event_pre('device-pre-attach:' + self._class, device)
@ -136,8 +145,8 @@ class DeviceCollection(object):
'''
if device not in self.attached():
raise KeyError(
'device {!r} of class {} not attached to {!r}'.format(
raise DeviceNotAttached(
'device {!s} of class {} not attached to {!s}'.format(
device, self._class, self._vm))
self._vm.fire_event_pre('device-pre-detach:' + self._class, device)
if persistent:
@ -261,6 +270,8 @@ class DeviceInfo(object):
self.ident == other.ident
)
def __str__(self):
return '{!s}:{!s}'.format(self.backend_domain, self.ident)
class UnknownDevice(DeviceInfo):
# pylint: disable=too-few-public-methods