forked from goodboy/tractor
1
0
Fork 0

Factor boxed-err formatting into new `pformat_boxed_tb()` helper for use elsewhere

runtime_to_msgspec
Tyler Goodlet 2024-04-05 14:04:32 -04:00
parent 7f1c2b8ecf
commit 5c1401bf81
1 changed files with 58 additions and 20 deletions

View File

@ -104,6 +104,57 @@ def get_err_type(type_name: str) -> BaseException|None:
return type_ref return type_ref
def pformat_boxed_tb(
tb_str: str,
fields_str: str|None = None,
field_prefix: str = ' |_',
indent: str = ' '*2
) -> str:
if (
fields_str
and
field_prefix
):
fields: str = textwrap.indent(
fields_str,
# prefix=' '*2,
# prefix=' |_',
prefix=field_prefix,
)
else:
fields = fields_str or ''
# body_indent: str = len(field_prefix) * ' '
body: str = (
# orig
# f' |\n'
# f' ------ - ------\n\n'
# f'{tb_str}\n'
# f' ------ - ------\n'
# f' _|\n'
f'|\n'
f' ------ - ------\n\n'
f'{tb_str}\n'
f' ------ - ------\n'
f'_|\n'
)
if len(indent):
body: str = textwrap.indent(
body,
# prefix=body_indent,
prefix=indent,
)
return (
fields
+
body
)
# return body
# TODO: rename to just `RemoteError`? # TODO: rename to just `RemoteError`?
class RemoteActorError(Exception): class RemoteActorError(Exception):
''' '''
@ -117,7 +168,7 @@ class RemoteActorError(Exception):
''' '''
reprol_fields: list[str] = [ reprol_fields: list[str] = [
'src_uid', 'src_uid',
'relay_path', # 'relay_path',
] ]
def __init__( def __init__(
@ -249,7 +300,7 @@ class RemoteActorError(Exception):
@property @property
def tb_str( def tb_str(
self, self,
indent: str = ' '*3, indent: str = ' ',
) -> str: ) -> str:
if remote_tb := self.msgdata.get('tb_str'): if remote_tb := self.msgdata.get('tb_str'):
return textwrap.indent( return textwrap.indent(
@ -309,24 +360,11 @@ class RemoteActorError(Exception):
fields: str = self._mk_fields_str( fields: str = self._mk_fields_str(
_body_fields, _body_fields,
) )
fields: str = textwrap.indent( body: str = pformat_boxed_tb(
fields, tb_str=self.tb_str,
# prefix=' '*2, fields_str=fields,
prefix=' |_', field_prefix=' |_',
) indent=' ', # no indent?
indent: str = ''*1
body: str = (
f'{fields}'
f' |\n'
f' ------ - ------\n\n'
f'{self.tb_str}\n'
f' ------ - ------\n'
f' _|\n'
)
if indent:
body: str = textwrap.indent(
body,
prefix=indent,
) )
return ( return (
f'<{type(self).__name__}(\n' f'<{type(self).__name__}(\n'