|
| 1 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | + |
| 3 | +# Copyright (C) 2020-2022 igo95862 |
| 4 | + |
| 5 | +# This file is part of python-sdbus |
| 6 | + |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | + |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | + |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from asyncio import get_running_loop, wait_for |
| 23 | +from typing import Any |
| 24 | + |
| 25 | +from sdbus.unittest import IsolatedDbusTestCase |
| 26 | + |
| 27 | +from sdbus import ( |
| 28 | + DbusFailedError, |
| 29 | + DbusInterfaceCommonAsync, |
| 30 | + dbus_method_async, |
| 31 | + request_default_bus_name_async, |
| 32 | +) |
| 33 | + |
| 34 | +HELLO_WORLD = 'Hello, world!' |
| 35 | + |
| 36 | + |
| 37 | +class DbusDeriveMethodError(DbusFailedError): |
| 38 | + dbus_error_name = 'org.example.Method.Error' |
| 39 | + |
| 40 | + |
| 41 | +class IndependentError(Exception): |
| 42 | + ... |
| 43 | + |
| 44 | + |
| 45 | +GOOD_STR = 'Good' |
| 46 | + |
| 47 | + |
| 48 | +class InterfaceWithErrors( |
| 49 | + DbusInterfaceCommonAsync, |
| 50 | + interface_name='org.example.test', |
| 51 | +): |
| 52 | + |
| 53 | + @dbus_method_async(result_signature='s') |
| 54 | + async def hello_independent_error(self) -> str: |
| 55 | + raise IndependentError |
| 56 | + |
| 57 | + @dbus_method_async(result_signature='s') |
| 58 | + async def hello_derrived_error(self) -> str: |
| 59 | + raise DbusDeriveMethodError |
| 60 | + |
| 61 | + |
| 62 | +class TestHighLevelErrors(IsolatedDbusTestCase): |
| 63 | + async def asyncSetUp(self) -> None: |
| 64 | + await super().asyncSetUp() |
| 65 | + |
| 66 | + await request_default_bus_name_async('org.test') |
| 67 | + self.test_object = InterfaceWithErrors() |
| 68 | + self.test_object.export_to_dbus('/') |
| 69 | + |
| 70 | + self.test_object_connection = InterfaceWithErrors.new_proxy( |
| 71 | + 'org.test', '/') |
| 72 | + |
| 73 | + loop = get_running_loop() |
| 74 | + |
| 75 | + def silence_exceptions(*args: Any, **kwrags: Any) -> None: |
| 76 | + ... |
| 77 | + |
| 78 | + loop.set_exception_handler(silence_exceptions) |
| 79 | + |
| 80 | + async def test_method_indenendent_error(self) -> None: |
| 81 | + with self.assertRaises(DbusFailedError) as cm: |
| 82 | + await wait_for( |
| 83 | + self.test_object_connection.hello_independent_error(), |
| 84 | + timeout=1, |
| 85 | + ) |
| 86 | + |
| 87 | + exception = cm.exception |
| 88 | + self.assertIs(exception.__class__, DbusFailedError) |
| 89 | + |
| 90 | + async def test_method_derived_error(self) -> None: |
| 91 | + with self.assertRaises(DbusDeriveMethodError): |
| 92 | + await wait_for( |
| 93 | + self.test_object_connection.hello_derrived_error(), |
| 94 | + timeout=1, |
| 95 | + ) |
0 commit comments