Skip to content

Commit 7e6c2ea

Browse files
committed
Custom raise helper
1 parent d66eadf commit 7e6c2ea

File tree

3 files changed

+212
-142
lines changed

3 files changed

+212
-142
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
"utc2local": "03_xtras.ipynb",
189189
"local2utc": "03_xtras.ipynb",
190190
"trace": "03_xtras.ipynb",
191+
"raise_with_msg": "03_xtras.ipynb",
191192
"round_multiple": "03_xtras.ipynb",
192193
"modified_env": "03_xtras.ipynb",
193194
"ContextManagers": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/03_xtras.ipynb (unless otherwise specified).
22

3+
4+
from __future__ import annotations
5+
6+
37
__all__ = ['dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection',
48
'walk', 'globtastic', 'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'loads', 'loads_multi',
59
'untar_dir', 'repo_details', 'run', 'open_file', 'save_pickle', 'load_pickle', 'get_source_link', 'truncstr',
610
'spark_chars', 'sparkline', 'autostart', 'EventTimer', 'stringfmt_names', 'PartialFormatter',
7-
'partial_format', 'utc2local', 'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers',
8-
'str2bool']
11+
'partial_format', 'utc2local', 'local2utc', 'trace', 'raise_with_msg', 'round_multiple', 'modified_env',
12+
'ContextManagers', 'str2bool']
13+
14+
# Cell
15+
#nbdev_comment from __future__ import annotations
916

1017
# Cell
1118
from .imports import *
@@ -467,6 +474,16 @@ def _inner(*args,**kwargs):
467474
_inner._traced = True
468475
return _inner
469476

477+
# Cell
478+
def raise_with_msg(
479+
e:Exception, # An exception
480+
msg:str|list=None, # A custom message
481+
replace:bool=False # Whether to replace e.args with [msg]
482+
):
483+
"Raises `e` with a custom message attached"
484+
e.args = listify(msg) if replace else listify(e.args) + listify(msg)
485+
raise e
486+
470487
# Cell
471488
def round_multiple(x, mult, round_down=False):
472489
"Round `x` to nearest multiple of `mult`"

0 commit comments

Comments
 (0)