Skip to content

Commit

Permalink
RSDK-5145 - better resource name representation, more info in unknown…
Browse files Browse the repository at this point in the history
… grpc errors (#445)
  • Loading branch information
cheukt authored Sep 26, 2023
1 parent a892aaa commit 5050a0e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/viam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ def _log_exceptions(exctype, value, traceback):
##################
# MONKEY PATCHES #
##################
_ResourceName.__hash__ = lambda self: hash(f"{self.namespace}:{self.type}:{self.subtype}/{self.name}")
_ResourceName.__str__ = lambda self: f"{self.namespace}:{self.type}:{self.subtype}/{self.name}"
_ResourceName.__repr__ = lambda self: f"<viam.proto.common.ResourceName {str(self)} at {hex(id(self))}>"
_ResourceName.__hash__ = lambda self: hash(str(self))
12 changes: 11 additions & 1 deletion src/viam/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,17 @@ async def interceptor(*args, **kwargs):
except ViamGRPCError as e:
raise e.grpc_error
except Exception as e:
raise GRPCError(Status.UNKNOWN, f"{e.__class__.__name__} - {e}")
tb = e.__traceback__
file_name = None
func_name = None
line_num = None
# only print the last entry in the stacktrace - not perfect but gives users a starting point
while tb is not None:
file_name = tb.tb_frame.f_code.co_filename
func_name = tb.tb_frame.f_code.co_name
line_num = tb.tb_lineno
tb = tb.tb_next
raise GRPCError(Status.UNKNOWN, f"{e.__class__.__name__} - {e} - {file_name=} {func_name=} {line_num=}")

return interceptor

Expand Down
2 changes: 1 addition & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def test_grpc_error_wrapper(self):
with pytest.raises(GRPCError) as e_info:
await wrapped_raise_exception()
assert e_info.value.args[0] == Status.UNKNOWN
assert e_info.value.args[1] == "Exception - this is a fake Exception"
assert "Exception - this is a fake Exception" in e_info.value.args[1]

with pytest.raises(ViamGRPCError) as e_info:
await raise_viamgrpcerror()
Expand Down

0 comments on commit 5050a0e

Please sign in to comment.