-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into enable-com
- Loading branch information
Showing
7 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import tempfile | ||
from buildstockbatch.utils import log_error_details, _str_repr | ||
import pytest | ||
import os | ||
|
||
|
||
def test_str_repr(): | ||
test_obj = [{1, 2, 3, 4, 5, 6}, {"List1": ["Item1", ('a', 'b', 'c', 'd'), "item3"], | ||
"long_name_list": ["long_name_one_two_three", "long_name"], | ||
"dict": {"key1": ["List_item1", "List_item2", "List_item3"], "Key2": "value2", | ||
"key3": "value3", "key4": "val4"}}] | ||
|
||
gen_repr = _str_repr(test_obj, list_max=2, dict_max=3, string_max=10) | ||
true_repr = "[{'1','2','3' ...6},{'List1': ['Item1',('a','b' ...4) ...3],'long_...14..._list': ['long_...23..."\ | ||
"three','long_name'],'dict': {'key1': ['List_item1','List_item2' ...3],'Key2': 'value2',"\ | ||
"'key3': 'value3' ...4}}]" | ||
|
||
assert true_repr == gen_repr | ||
|
||
|
||
def test_get_error_details(): | ||
tf = tempfile.NamedTemporaryFile('w+', delete=False) | ||
tf.close() | ||
|
||
@log_error_details(tf.name) | ||
def failing_function1(arg1): | ||
level_1_string = f"string1_{arg1}" | ||
level_1_dict = {"key1": "value1"} | ||
|
||
def failing_function2(arg2): | ||
level_2_string = f"string2_{arg2}" | ||
level_2_list = ["level_2_str1", "level_2_str2"] | ||
if level_2_string and level_2_list: | ||
raise KeyError("actual dummy exception") | ||
|
||
if level_1_dict and level_1_string: | ||
failing_function2("my_arg2") | ||
|
||
with pytest.raises(KeyError) as ex_info: | ||
failing_function1("my_arg1") | ||
|
||
assert "actual dummy exception" in str(ex_info.value) | ||
with open(tf.name, 'r') as f: | ||
error_log = f.read() | ||
assert "'arg1':'my_arg1'" in error_log | ||
assert "'level_1_string':'string1_my_arg1'" in error_log | ||
assert "'level_1_dict':{'key1': 'value1'}" in error_log | ||
assert "'arg2':'my_arg2'" in error_log | ||
assert "'level_2_string':'string2_my_arg2'" in error_log | ||
assert "'level_2_list':['level_2_str1','level_2_str2']" in error_log | ||
os.remove(tf.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import traceback | ||
import inspect | ||
|
||
|
||
def _str_repr(obj, list_max=20, dict_max=20, string_max=100): | ||
if type(obj) is str: | ||
if len(obj) <= string_max: | ||
return f"'{obj}'" | ||
else: | ||
return f"'{obj[0:string_max//2]}...{len(obj)}...{obj[-string_max//2:]}'" | ||
elif type(obj) in [int, float]: | ||
return _str_repr(str(obj), list_max, dict_max, string_max) | ||
elif type(obj) is list: | ||
txt = "[" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:list_max]]) | ||
if len(obj) > list_max: | ||
txt += f" ...{len(obj)}" | ||
txt += "]" | ||
return txt | ||
elif type(obj) is tuple: | ||
txt = "(" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:list_max]]) | ||
if len(obj) > list_max: | ||
txt += f" ...{len(obj)}" | ||
txt += ")" | ||
return txt | ||
elif type(obj) is set: | ||
obj = list(obj) | ||
txt = "{" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:dict_max]]) | ||
if len(obj) > dict_max: | ||
txt += f" ...{len(obj)}" | ||
txt += "}" | ||
return txt | ||
elif type(obj) is dict: | ||
keys = list(obj.keys()) | ||
txt = "{" + ",".join([f"{_str_repr(key, list_max, dict_max, string_max)}:" | ||
f" {_str_repr(obj[key], list_max, dict_max, string_max)}" for key in keys[0:dict_max]]) | ||
if len(keys) > dict_max: | ||
txt += f" ...{len(keys)}" | ||
txt += "}" | ||
return txt | ||
else: | ||
return str(obj) | ||
|
||
|
||
def _get_error_details(): | ||
text = "" | ||
text += traceback.format_exc() | ||
frames = inspect.trace() | ||
for frame in frames: | ||
text += f'\nIn file: {frame[1]}, module {str(frame[3])} line: {frame[2]} \n' | ||
text += "Local Variables: " | ||
for var, value in frame[0].f_locals.items(): | ||
text += _str_repr(var) + ":" + _str_repr(value) | ||
text += "\n" | ||
return text | ||
|
||
|
||
def log_error_details(output_file="buildstockbatch_crash_details.log"): | ||
def log_error_decorator(func): | ||
def run_with_error_capture(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception: | ||
with open(output_file, "w") as f: | ||
f.write(_get_error_details()) | ||
raise | ||
return run_with_error_capture | ||
|
||
return log_error_decorator |