From e1de82ea53a9e6f8b5211d62c6851873e0c56ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 3 Sep 2016 02:13:02 +0200 Subject: [PATCH] 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 --- qubes/devices.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/qubes/devices.py b/qubes/devices.py index 28f63c61..36fe4344 100644 --- a/qubes/devices.py +++ b/qubes/devices.py @@ -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