|
|
372936 |
From e3c082e3619ab2d9095b58e55c90157f6376fd1e Mon Sep 17 00:00:00 2001
|
|
|
372936 |
From: Vendula Poncova <vponcova@redhat.com>
|
|
|
372936 |
Date: Thu, 16 Jul 2020 20:24:25 +0200
|
|
|
372936 |
Subject: [PATCH 2/2] Handle all errors of the DBus call
|
|
|
372936 |
|
|
|
372936 |
Don't handle only errors caused by the method invocation, but handle
|
|
|
372936 |
also errors caused by processing the result of the method.
|
|
|
372936 |
---
|
|
|
372936 |
dasbus/client/handler.py | 3 +--
|
|
|
372936 |
dasbus/server/handler.py | 11 +++++------
|
|
|
372936 |
tests/test_client.py | 16 ++++++++++++++++
|
|
|
372936 |
tests/test_server.py | 36 ++++++++++++++++++++++++++++++------
|
|
|
372936 |
4 files changed, 52 insertions(+), 14 deletions(-)
|
|
|
372936 |
|
|
|
372936 |
diff --git a/dasbus/client/handler.py b/dasbus/client/handler.py
|
|
|
372936 |
index ed1a690..839f5a6 100644
|
|
|
372936 |
--- a/dasbus/client/handler.py
|
|
|
372936 |
+++ b/dasbus/client/handler.py
|
|
|
372936 |
@@ -472,10 +472,9 @@ class ClientObjectHandler(AbstractClientObjectHandler):
|
|
|
372936 |
"""
|
|
|
372936 |
try:
|
|
|
372936 |
result = call(*args, **kwargs)
|
|
|
372936 |
+ return self._handle_method_result(result)
|
|
|
372936 |
except Exception as error: # pylint: disable=broad-except
|
|
|
372936 |
return self._handle_method_error(error)
|
|
|
372936 |
- else:
|
|
|
372936 |
- return self._handle_method_result(result)
|
|
|
372936 |
|
|
|
372936 |
def _handle_method_error(self, error):
|
|
|
372936 |
"""Handle an error of a DBus call.
|
|
|
372936 |
diff --git a/dasbus/server/handler.py b/dasbus/server/handler.py
|
|
|
372936 |
index 3871948..8183f26 100644
|
|
|
372936 |
--- a/dasbus/server/handler.py
|
|
|
372936 |
+++ b/dasbus/server/handler.py
|
|
|
372936 |
@@ -420,6 +420,11 @@ class ServerObjectHandler(AbstractServerObjectHandler):
|
|
|
372936 |
method_name,
|
|
|
372936 |
*unwrap_variant(parameters)
|
|
|
372936 |
)
|
|
|
372936 |
+ self._handle_method_result(
|
|
|
372936 |
+ invocation,
|
|
|
372936 |
+ member,
|
|
|
372936 |
+ result
|
|
|
372936 |
+ )
|
|
|
372936 |
except Exception as error: # pylint: disable=broad-except
|
|
|
372936 |
self._handle_method_error(
|
|
|
372936 |
invocation,
|
|
|
372936 |
@@ -427,12 +432,6 @@ class ServerObjectHandler(AbstractServerObjectHandler):
|
|
|
372936 |
method_name,
|
|
|
372936 |
error
|
|
|
372936 |
)
|
|
|
372936 |
- else:
|
|
|
372936 |
- self._handle_method_result(
|
|
|
372936 |
- invocation,
|
|
|
372936 |
- member,
|
|
|
372936 |
- result
|
|
|
372936 |
- )
|
|
|
372936 |
|
|
|
372936 |
def _handle_method_error(self, invocation, interface_name, method_name,
|
|
|
372936 |
error):
|
|
|
372936 |
diff --git a/tests/test_client.py b/tests/test_client.py
|
|
|
372936 |
index 7cb96c3..cd44552 100644
|
|
|
372936 |
--- a/tests/test_client.py
|
|
|
372936 |
+++ b/tests/test_client.py
|
|
|
372936 |
@@ -256,6 +256,22 @@ class DBusClientTestCase(unittest.TestCase):
|
|
|
372936 |
str(cm.exception)
|
|
|
372936 |
)
|
|
|
372936 |
|
|
|
372936 |
+ def test_invalid_method_result(self):
|
|
|
372936 |
+ """Test a method proxy with an invalid result."""
|
|
|
372936 |
+ self._create_proxy("""
|
|
|
372936 |
+ <node>
|
|
|
372936 |
+ <interface name="Interface">
|
|
|
372936 |
+ <method name="Method">
|
|
|
372936 |
+ <arg direction="out" name="return" type="t"/>
|
|
|
372936 |
+ </method>
|
|
|
372936 |
+ </interface>
|
|
|
372936 |
+ </node>
|
|
|
372936 |
+ """)
|
|
|
372936 |
+
|
|
|
372936 |
+ self._set_reply(get_variant("i", -1))
|
|
|
372936 |
+ with self.assertRaises(TypeError):
|
|
|
372936 |
+ self.proxy.Method()
|
|
|
372936 |
+
|
|
|
372936 |
def _set_reply(self, reply_value):
|
|
|
372936 |
"""Set the reply of the DBus call."""
|
|
|
372936 |
self.connection.call_sync.reset_mock()
|
|
|
372936 |
diff --git a/tests/test_server.py b/tests/test_server.py
|
|
|
372936 |
index ce91b97..4adcf89 100644
|
|
|
372936 |
--- a/tests/test_server.py
|
|
|
372936 |
+++ b/tests/test_server.py
|
|
|
372936 |
@@ -82,8 +82,8 @@ class DBusServerTestCase(unittest.TestCase):
|
|
|
372936 |
|
|
|
372936 |
def _call_method_with_error(self, interface, method,
|
|
|
372936 |
parameters=NO_PARAMETERS,
|
|
|
372936 |
- error_name="",
|
|
|
372936 |
- error_message=""):
|
|
|
372936 |
+ error_name=None,
|
|
|
372936 |
+ error_message=None):
|
|
|
372936 |
invocation = Mock()
|
|
|
372936 |
|
|
|
372936 |
with self.assertLogs(level='WARN'):
|
|
|
372936 |
@@ -94,12 +94,17 @@ class DBusServerTestCase(unittest.TestCase):
|
|
|
372936 |
parameters
|
|
|
372936 |
)
|
|
|
372936 |
|
|
|
372936 |
- invocation.return_dbus_error.assert_called_once_with(
|
|
|
372936 |
- error_name,
|
|
|
372936 |
- error_message
|
|
|
372936 |
- )
|
|
|
372936 |
+ invocation.return_dbus_error.assert_called_once()
|
|
|
372936 |
invocation.return_value.assert_not_called()
|
|
|
372936 |
|
|
|
372936 |
+ (name, msg), kwargs = invocation.return_dbus_error.call_args
|
|
|
372936 |
+
|
|
|
372936 |
+ self.assertEqual(kwargs, {})
|
|
|
372936 |
+ self.assertEqual(name, error_name, "Unexpected error name.")
|
|
|
372936 |
+
|
|
|
372936 |
+ if error_message is not None:
|
|
|
372936 |
+ self.assertEqual(msg, error_message, "Unexpected error message.")
|
|
|
372936 |
+
|
|
|
372936 |
def test_register(self):
|
|
|
372936 |
"""Test the object registration."""
|
|
|
372936 |
with self.assertRaises(DBusSpecificationError) as cm:
|
|
|
372936 |
@@ -193,6 +198,25 @@ class DBusServerTestCase(unittest.TestCase):
|
|
|
372936 |
error_message="The method has failed."
|
|
|
372936 |
)
|
|
|
372936 |
|
|
|
372936 |
+ def test_invalid_method_result(self):
|
|
|
372936 |
+ """Test a method with an invalid result."""
|
|
|
372936 |
+ self._publish_object("""
|
|
|
372936 |
+ <node>
|
|
|
372936 |
+ <interface name="Interface">
|
|
|
372936 |
+ <method name="Method">
|
|
|
372936 |
+ <arg direction="out" name="return" type="t"/>
|
|
|
372936 |
+ </method>
|
|
|
372936 |
+ </interface>
|
|
|
372936 |
+ </node>
|
|
|
372936 |
+ """)
|
|
|
372936 |
+
|
|
|
372936 |
+ self.object.Method.return_value = -1
|
|
|
372936 |
+ self._call_method_with_error(
|
|
|
372936 |
+ "Interface",
|
|
|
372936 |
+ "Method",
|
|
|
372936 |
+ error_name="not.known.Error.OverflowError"
|
|
|
372936 |
+ )
|
|
|
372936 |
+
|
|
|
372936 |
def test_property(self):
|
|
|
372936 |
"""Test the property publishing."""
|
|
|
372936 |
self._publish_object("""
|
|
|
372936 |
--
|
|
|
372936 |
2.26.2
|
|
|
372936 |
|