diff --git a/src/roswire/exceptions.py b/src/roswire/exceptions.py index 1ce3ba49..1443db57 100644 --- a/src/roswire/exceptions.py +++ b/src/roswire/exceptions.py @@ -6,6 +6,30 @@ class ROSWireException(Exception): """Base class used by all ROSWire exceptions.""" +@_attr.s(frozen=True, auto_exc=True, auto_attribs=True) +class UnexpectedServiceCallError(ROSWireException): + """An unexpected error occurred during a service call. + + Attributes + ---------- + service_name: str + The name of the service that was called. + retcode: int + The return code that was produced by rosservice. + output: str + The output that was produced by rosservice + """ + service_name: str + retcode: int + output: str + + def __str__(self) -> str: + m = (f"Unexpected exit code [{self.retcode}] occurred " + f" during call to service [{self.service_name}]. " + f"Produced output: \"{self.output}\"") + return m + + class FailedToParseLaunchFile(ROSWireException): """An attempt to parse a launch file failed.""" diff --git a/src/roswire/proxy/service.py b/src/roswire/proxy/service.py index bee08c0d..278647dc 100644 --- a/src/roswire/proxy/service.py +++ b/src/roswire/proxy/service.py @@ -13,6 +13,7 @@ from .shell import ShellProxy from .. import exceptions +from ..exceptions import UnexpectedServiceCallError from ..definitions import Message, SrvFormat, MsgFormat from ..description import SystemDescription @@ -51,6 +52,13 @@ def call(self, message: Optional[Message] = None) -> Optional[Message]: ------- Optional[Message] The reply produced by the service, if any. + + Raises + ------ + ROSWireException + If illegal arguments are provided to the service call. + UnexpectedServiceCallError + If an unexpected error occurred during the service call. """ if not message: yml = '{}' @@ -62,7 +70,9 @@ def call(self, message: Optional[Message] = None) -> Optional[Message]: if code == 2: raise exceptions.ROSWireException('illegal service call args') if code != 0: - raise exceptions.ROSWireException('unexpected error during service call') # noqa + raise UnexpectedServiceCallError(service_name=self.name, + retcode=code, + output=output) fmt_response: Optional[MsgFormat] = self.format.response if not fmt_response: