MCPcopy Create free account
hub / github.com/Tencent/CodeAnalysis / ErrorCatcher

Class ErrorCatcher

server/projects/main/util/scm/errorcatch.py:13–58  ·  view source on GitHub ↗

Catch an object method"s exception and handle the exception with custom error handler. The custom error handler is a callable accept an argument ``error``:: def my_error_handler(error): if isinstance(error, Exception): #return Exception subclass instance if wan

Source from the content-addressed store, hash-verified

11
12
13class ErrorCatcher(object):
14 """Catch an object method"s exception and handle the exception with custom error handler.
15
16 The custom error handler is a callable accept an argument ``error``::
17
18 def my_error_handler(error):
19 if isinstance(error, Exception):
20 #return Exception subclass instance if want to raise
21 #else just return a value to caller
22 else:
23 #do else thing
24 """
25
26 def __init__(self, obj, error_handler):
27 """
28 :param obj: object to catch error
29 :param error_handler: custom error handler
30 """
31
32 self._obj = obj
33 self._err_handler = error_handler
34
35 def __getattr__(self, name):
36 try:
37 value = getattr(self._obj, name)
38 except Exception as err: # NOCA:broad-except(可能存在多种异常)
39 rst = self._err_handler(err)
40 if isinstance(rst, Exception):
41 raise rst
42 else:
43 return rst
44 else:
45 if not hasattr(value, "__call__"):
46 return value
47 else:
48 def _callwrap(*args, **kwargs):
49 try:
50 return value(*args, **kwargs)
51 except Exception as err: # NOCA:broad-except(可能存在多种异常)
52 rst = self._err_handler(err)
53 if isinstance(rst, Exception):
54 raise rst
55 else:
56 return rst
57
58 return _callwrap

Callers 1

ScmClientFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected