Skip to content

Commit 75c39d3

Browse files
committed
feat: improve strategy error warning
closes freqtrade#12418
1 parent 911a803 commit 75c39d3

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

freqtrade/strategy/strategy_wrapper.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
F = TypeVar("F", bound=Callable[..., Any])
1414

1515

16+
def __format_traceback(error: Exception) -> str:
17+
"""Format the traceback of an exception into a formatted string."""
18+
tb = error.__traceback__
19+
while tb:
20+
if tb.tb_frame.f_code.co_filename == __file__:
21+
# Skip frames from this file
22+
tb = tb.tb_next
23+
continue
24+
return f"{tb.tb_frame.f_code.co_qualname}:{tb.tb_lineno}"
25+
26+
return ""
27+
28+
1629
def strategy_safe_wrapper(f: F, message: str = "", default_retval=None, supress_error=False) -> F:
1730
"""
1831
Wrapper around user-provided methods and functions.
@@ -30,12 +43,17 @@ def wrapper(*args, **kwargs):
3043
kwargs["trade"] = deepcopy(kwargs["trade"])
3144
return f(*args, **kwargs)
3245
except ValueError as error:
33-
logger.warning(f"{message}Strategy caused the following exception: {error}{f}")
46+
traceback = __format_traceback(error)
47+
name = f.__name__ if hasattr(f, "__name__") else str(f)
48+
logger.warning(
49+
f"{message}Strategy caused the following exception: {repr(error)} in "
50+
f"{traceback}, calling {name}",
51+
)
3452
if default_retval is None and not supress_error:
3553
raise StrategyError(str(error)) from error
3654
return default_retval
3755
except Exception as error:
38-
logger.exception(f"{message}Unexpected error {error} calling {f}")
56+
logger.exception(f"{message}Unexpected error {repr(error)} calling {f}")
3957
if default_retval is None and not supress_error:
4058
raise StrategyError(str(error)) from error
4159
return default_retval

0 commit comments

Comments
 (0)