MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / log_if_error

Function log_if_error

example_code/item_087.py:105–120  ·  view source on GitHub ↗
(file_path, target, *args, **kwargs)

Source from the content-addressed store, hash-verified

103import json
104
105def log_if_error(file_path, target, *args, **kwargs):
106 try:
107 target(*args, **kwargs)
108 except BaseException as e:
109 stack = traceback.extract_tb(e.__traceback__)
110 stack_without_wrapper = stack[1:]
111 trace_dict = dict(
112 stack=[item.name for item in stack_without_wrapper],
113 error_type=type(e).__name__,
114 error_message=str(e),
115 )
116 json_data = json.dumps(trace_dict)
117
118 with open(file_path, "a") as f:
119 f.write(json_data)
120 f.write("\n")
121
122
123print("Example 6")

Callers 1

item_087.pyFile · 0.85

Calls 2

openFunction · 0.85
writeMethod · 0.80

Tested by

no test coverage detected