Skip to content

Commit d0cad42

Browse files
committed
Add utility for chaining exceptions
1 parent 22ba9ff commit d0cad42

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Trading/utils/chain_exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Callable, Any, List
2+
3+
4+
def chain_exceptions(
5+
function_to_apply: Callable[..., Any],
6+
exceptions: List[Exception],
7+
default_return=None,
8+
*args,
9+
**kwargs
10+
) -> Any:
11+
'''
12+
Chain exceptions in a way that the function_to_apply is executed and if it raises an exception,
13+
the exception is appended to the exceptions list and the default_return is returned.
14+
'''
15+
try:
16+
return function_to_apply(*args, **kwargs)
17+
except Exception as e:
18+
exceptions.append(e)
19+
return default_return

0 commit comments

Comments
 (0)