2018-11-19 08:51:12 +00:00
|
|
|
"""
|
|
|
|
Our classy exception set.
|
|
|
|
"""
|
2019-01-01 20:58:38 +00:00
|
|
|
import importlib
|
2018-11-19 08:51:12 +00:00
|
|
|
import builtins
|
|
|
|
import traceback
|
|
|
|
|
2019-10-30 04:32:10 +00:00
|
|
|
import trio
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
|
2019-01-01 20:58:38 +00:00
|
|
|
_this_mod = importlib.import_module(__name__)
|
|
|
|
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
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)
|
2019-10-30 04:32:10 +00:00
|
|
|
for ns in [builtins, _this_mod, trio]:
|
2019-01-01 20:58:38 +00:00
|
|
|
try:
|
|
|
|
self.type = getattr(ns, type_str)
|
|
|
|
break
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.type = Exception
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
2019-01-12 22:55:28 +00:00
|
|
|
class ModuleNotExposed(ModuleNotFoundError):
|
2019-01-01 20:58:38 +00:00
|
|
|
"The requested module is not exposed for RPC"
|
|
|
|
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
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__,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-12 18:26:02 +00:00
|
|
|
def unpack_error(msg, chan=None, err_type=RemoteActorError):
|
2018-11-19 08:51:12 +00:00
|
|
|
"""Unpack an 'error' message from the wire
|
|
|
|
into a local ``RemoteActorError``.
|
|
|
|
"""
|
|
|
|
tb_str = msg['error'].get('tb_str', '')
|
2019-01-12 18:26:02 +00:00
|
|
|
return err_type(
|
2018-11-19 08:51:12 +00:00
|
|
|
f"{chan.uid}\n" + tb_str,
|
|
|
|
**msg['error'],
|
|
|
|
)
|