Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1 | From 69968dec867053e38de0b91d76ac41d5a5735e36 Mon Sep 17 00:00:00 2001 |
| 2 | From: Vendula Poncova <vponcova@redhat.com> |
| 3 | Date: Thu, 2 Aug 2018 15:31:56 +0800 |
| 4 | Subject: [PATCH 2/2] Support transformation between D-Bus errors and |
| 5 | exceptions. |
| 6 | |
| 7 | Exceptions can be registered with decorators, raised in a remote |
| 8 | method and recreated after return from the remote call. |
| 9 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 10 | Upstream-Status: Backport [https://src.fedoraproject.org/cgit/rpms/python-pydbus.git/] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | |
| 12 | Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com> |
| 13 | --- |
| 14 | pydbus/error.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 15 | pydbus/proxy_method.py | 18 ++++++++-- |
| 16 | pydbus/registration.py | 16 ++++++--- |
| 17 | 3 files changed, 123 insertions(+), 8 deletions(-) |
| 18 | create mode 100644 pydbus/error.py |
| 19 | |
| 20 | diff --git a/pydbus/error.py b/pydbus/error.py |
| 21 | new file mode 100644 |
| 22 | index 0000000..aaa3510 |
| 23 | --- /dev/null |
| 24 | +++ b/pydbus/error.py |
| 25 | @@ -0,0 +1,97 @@ |
| 26 | +from gi.repository import GLib, Gio |
| 27 | + |
| 28 | + |
| 29 | +def register_error(name, domain, code): |
| 30 | + """Register and map decorated exception class to a DBus error.""" |
| 31 | + def decorated(cls): |
| 32 | + error_registration.register_error(cls, name, domain, code) |
| 33 | + return cls |
| 34 | + |
| 35 | + return decorated |
| 36 | + |
| 37 | + |
| 38 | +def map_error(error_name): |
| 39 | + """Map decorated exception class to a DBus error.""" |
| 40 | + def decorated(cls): |
| 41 | + error_registration.map_error(cls, error_name) |
| 42 | + return cls |
| 43 | + |
| 44 | + return decorated |
| 45 | + |
| 46 | + |
| 47 | +def map_by_default(cls): |
| 48 | + """Map decorated exception class to all unknown DBus errors.""" |
| 49 | + error_registration.map_by_default(cls) |
| 50 | + return cls |
| 51 | + |
| 52 | + |
| 53 | +class ErrorRegistration(object): |
| 54 | + """Class for mapping exceptions to DBus errors.""" |
| 55 | + |
| 56 | + _default = None |
| 57 | + _map = dict() |
| 58 | + _reversed_map = dict() |
| 59 | + |
| 60 | + def map_by_default(self, exception_cls): |
| 61 | + """Set the exception class as a default.""" |
| 62 | + self._default = exception_cls |
| 63 | + |
| 64 | + def map_error(self, exception_cls, name): |
| 65 | + """Map the exception class to a DBus name.""" |
| 66 | + self._map[name] = exception_cls |
| 67 | + self._reversed_map[exception_cls] = name |
| 68 | + |
| 69 | + def register_error(self, exception_cls, name, domain, code): |
| 70 | + """Map and register the exception class to a DBus name.""" |
| 71 | + self.map_error(exception_cls, name) |
| 72 | + return Gio.DBusError.register_error(domain, code, name) |
| 73 | + |
| 74 | + def is_registered_exception(self, obj): |
| 75 | + """Is the exception registered?""" |
| 76 | + return obj.__class__ in self._reversed_map |
| 77 | + |
| 78 | + def get_dbus_name(self, obj): |
| 79 | + """Get the DBus name of the exception.""" |
| 80 | + return self._reversed_map.get(obj.__class__) |
| 81 | + |
| 82 | + def get_exception_class(self, name): |
| 83 | + """Get the exception class mapped to the DBus name.""" |
| 84 | + return self._map.get(name, self._default) |
| 85 | + |
| 86 | + def transform_message(self, name, message): |
| 87 | + """Transform the message of the exception.""" |
| 88 | + prefix = "{}:{}: ".format("GDBus.Error", name) |
| 89 | + |
| 90 | + if message.startswith(prefix): |
| 91 | + return message[len(prefix):] |
| 92 | + |
| 93 | + return message |
| 94 | + |
| 95 | + def transform_exception(self, e): |
| 96 | + """Transform the remote error to the exception.""" |
| 97 | + if not isinstance(e, GLib.Error): |
| 98 | + return e |
| 99 | + |
| 100 | + if not Gio.DBusError.is_remote_error(e): |
| 101 | + return e |
| 102 | + |
| 103 | + # Get DBus name of the error. |
| 104 | + name = Gio.DBusError.get_remote_error(e) |
| 105 | + # Get the exception class. |
| 106 | + exception_cls = self.get_exception_class(name) |
| 107 | + |
| 108 | + # Return the original exception. |
| 109 | + if not exception_cls: |
| 110 | + return e |
| 111 | + |
| 112 | + # Return new exception. |
| 113 | + message = self.transform_message(name, e.message) |
| 114 | + exception = exception_cls(message) |
| 115 | + exception.dbus_name = name |
| 116 | + exception.dbus_domain = e.domain |
| 117 | + exception.dbus_code = e.code |
| 118 | + return exception |
| 119 | + |
| 120 | + |
| 121 | +# Default error registration. |
| 122 | +error_registration = ErrorRegistration() |
| 123 | diff --git a/pydbus/proxy_method.py b/pydbus/proxy_method.py |
| 124 | index 4ea4304..e9496f5 100644 |
| 125 | --- a/pydbus/proxy_method.py |
| 126 | +++ b/pydbus/proxy_method.py |
| 127 | @@ -2,6 +2,7 @@ from gi.repository import GLib |
| 128 | from .generic import bound_method |
| 129 | from .identifier import filter_identifier |
| 130 | from .timeout import timeout_to_glib |
| 131 | +from .error import error_registration |
| 132 | |
| 133 | try: |
| 134 | from inspect import Signature, Parameter |
| 135 | @@ -87,9 +88,20 @@ class ProxyMethod(object): |
| 136 | call_args += (self._finish_async_call, (callback, callback_args)) |
| 137 | instance._bus.con.call(*call_args) |
| 138 | return None |
| 139 | + |
| 140 | else: |
| 141 | - ret = instance._bus.con.call_sync(*call_args) |
| 142 | - return self._unpack_return(ret) |
| 143 | + result = None |
| 144 | + error = None |
| 145 | + |
| 146 | + try: |
| 147 | + result = instance._bus.con.call_sync(*call_args) |
| 148 | + except Exception as e: |
| 149 | + error = error_registration.transform_exception(e) |
| 150 | + |
| 151 | + if error: |
| 152 | + raise error |
| 153 | + |
| 154 | + return self._unpack_return(result) |
| 155 | |
| 156 | def _unpack_return(self, values): |
| 157 | ret = values.unpack() |
| 158 | @@ -108,7 +120,7 @@ class ProxyMethod(object): |
| 159 | ret = source.call_finish(result) |
| 160 | return_args = self._unpack_return(ret) |
| 161 | except Exception as err: |
| 162 | - error = err |
| 163 | + error = error_registration.transform_exception(err) |
| 164 | |
| 165 | callback, callback_args = user_data |
| 166 | callback(*callback_args, returned=return_args, error=error) |
| 167 | diff --git a/pydbus/registration.py b/pydbus/registration.py |
| 168 | index f531539..1d2cbcb 100644 |
| 169 | --- a/pydbus/registration.py |
| 170 | +++ b/pydbus/registration.py |
| 171 | @@ -5,6 +5,7 @@ from . import generic |
| 172 | from .exitable import ExitableWithAliases |
| 173 | from functools import partial |
| 174 | from .method_call_context import MethodCallContext |
| 175 | +from .error import error_registration |
| 176 | import logging |
| 177 | |
| 178 | try: |
| 179 | @@ -91,11 +92,16 @@ class ObjectWrapper(ExitableWithAliases("unwrap")): |
| 180 | logger = logging.getLogger(__name__) |
| 181 | logger.exception("Exception while handling %s.%s()", interface_name, method_name) |
| 182 | |
| 183 | - #TODO Think of a better way to translate Python exception types to DBus error types. |
| 184 | - e_type = type(e).__name__ |
| 185 | - if not "." in e_type: |
| 186 | - e_type = "unknown." + e_type |
| 187 | - invocation.return_dbus_error(e_type, str(e)) |
| 188 | + if error_registration.is_registered_exception(e): |
| 189 | + name = error_registration.get_dbus_name(e) |
| 190 | + invocation.return_dbus_error(name, str(e)) |
| 191 | + else: |
| 192 | + logger.info("name is not registered") |
| 193 | + e_type = type(e).__name__ |
| 194 | + if not "." in e_type: |
| 195 | + e_type = "unknown." + e_type |
| 196 | + |
| 197 | + invocation.return_dbus_error(e_type, str(e)) |
| 198 | |
| 199 | def Get(self, interface_name, property_name): |
| 200 | type = self.readable_properties[interface_name + "." + property_name] |
| 201 | -- |
| 202 | 2.7.4 |
| 203 | |