From 2f6609ab7821d04a1e43c6176e6398508fa17cd9 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 19 Nov 2018 03:51:12 -0500 Subject: [PATCH] Add custom exceptions with msg (un)packing --- tractor/_exceptions.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tractor/_exceptions.py diff --git a/tractor/_exceptions.py b/tractor/_exceptions.py new file mode 100644 index 0000000..7244396 --- /dev/null +++ b/tractor/_exceptions.py @@ -0,0 +1,50 @@ +""" +Our classy exception set. +""" +import builtins +import traceback + + +class RemoteActorError(Exception): + # TODO: local recontruction of remote exception deats + "Remote actor exception bundled locally" + def __init__(self, message, type_str, **msgdata): + super().__init__(message) + self.type = getattr(builtins, type_str, Exception) + self.msgdata = msgdata + + # TODO: a trio.MultiError.catch like context manager + # for catching underlying remote errors of a particular type + + +class InternalActorError(RemoteActorError): + """Remote internal ``tractor`` error indicating + failure of some primitive or machinery. + """ + + +class NoResult(RuntimeError): + "No final result is expected for this actor" + + +def pack_error(exc): + """Create an "error message" for tranmission over + a channel (aka the wire). + """ + return { + 'error': { + 'tb_str': traceback.format_exc(), + 'type_str': type(exc).__name__, + } + } + + +def unpack_error(msg, chan=None): + """Unpack an 'error' message from the wire + into a local ``RemoteActorError``. + """ + tb_str = msg['error'].get('tb_str', '') + return RemoteActorError( + f"{chan.uid}\n" + tb_str, + **msg['error'], + )