Skip to content

Commit d604ccc

Browse files
committed
Don't throw
1 parent 7e6c2ea commit d604ccc

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"utc2local": "03_xtras.ipynb",
189189
"local2utc": "03_xtras.ipynb",
190190
"trace": "03_xtras.ipynb",
191-
"raise_with_msg": "03_xtras.ipynb",
191+
"modify_exception": "03_xtras.ipynb",
192192
"round_multiple": "03_xtras.ipynb",
193193
"modified_env": "03_xtras.ipynb",
194194
"ContextManagers": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
'walk', 'globtastic', 'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'loads', 'loads_multi',
99
'untar_dir', 'repo_details', 'run', 'open_file', 'save_pickle', 'load_pickle', 'get_source_link', 'truncstr',
1010
'spark_chars', 'sparkline', 'autostart', 'EventTimer', 'stringfmt_names', 'PartialFormatter',
11-
'partial_format', 'utc2local', 'local2utc', 'trace', 'raise_with_msg', 'round_multiple', 'modified_env',
11+
'partial_format', 'utc2local', 'local2utc', 'trace', 'modify_exception', 'round_multiple', 'modified_env',
1212
'ContextManagers', 'str2bool']
1313

1414
# Cell
@@ -475,14 +475,14 @@ def _inner(*args,**kwargs):
475475
return _inner
476476

477477
# Cell
478-
def raise_with_msg(
478+
def modify_exception(
479479
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
480+
msg:str=None, # A custom message
481+
replace:bool=False, # Whether to replace e.args with [msg]
482+
) -> Exception:
483+
"Modifies `e` with a custom message attached"
484+
e.args = [f'{e.args[0]} {msg}'] if not replace and len(e.args) > 0 else [msg]
485+
return e
486486

487487
# Cell
488488
def round_multiple(x, mult, round_down=False):

nbs/03_xtras.ipynb

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": 2,
14+
"execution_count": 1,
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
@@ -21,7 +21,7 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": 3,
24+
"execution_count": 2,
2525
"metadata": {},
2626
"outputs": [],
2727
"source": [
@@ -44,7 +44,7 @@
4444
},
4545
{
4646
"cell_type": "code",
47-
"execution_count": 4,
47+
"execution_count": 3,
4848
"metadata": {},
4949
"outputs": [],
5050
"source": [
@@ -2100,32 +2100,42 @@
21002100
},
21012101
{
21022102
"cell_type": "code",
2103-
"execution_count": 133,
2103+
"execution_count": null,
21042104
"metadata": {},
21052105
"outputs": [],
2106+
"source": []
2107+
},
2108+
{
2109+
"cell_type": "code",
2110+
"execution_count": 158,
2111+
"metadata": {
2112+
"tags": []
2113+
},
2114+
"outputs": [],
21062115
"source": [
21072116
"#|export\n",
2108-
"def raise_with_msg(\n",
2117+
"def modify_exception(\n",
21092118
" e:Exception, # An exception\n",
2110-
" msg:str|list=None, # A custom message\n",
2111-
" replace:bool=False # Whether to replace e.args with [msg]\n",
2112-
"):\n",
2113-
" \"Raises `e` with a custom message attached\"\n",
2114-
" e.args = listify(msg) if replace else listify(e.args) + listify(msg)\n",
2115-
" raise e"
2119+
" msg:str=None, # A custom message\n",
2120+
" replace:bool=False, # Whether to replace e.args with [msg]\n",
2121+
") -> Exception:\n",
2122+
" \"Modifies `e` with a custom message attached\"\n",
2123+
" e.args = [f'{e.args[0]} {msg}'] if not replace and len(e.args) > 0 else [msg]\n",
2124+
" return e"
21162125
]
21172126
},
21182127
{
21192128
"cell_type": "code",
2120-
"execution_count": 132,
2129+
"execution_count": 164,
21212130
"metadata": {},
21222131
"outputs": [],
21232132
"source": [
21242133
"msg = \"This is my custom message!\"\n",
2125-
"test_fail(lambda: raise_with_msg(Exception(), None), contains='')\n",
2126-
"test_fail(lambda: raise_with_msg(Exception(), msg), contains=msg)\n",
2127-
"test_fail(lambda: raise_with_msg(Exception(\"The first message\"), msg), contains=\"('The first message', 'This is my custom message!')\")\n",
2128-
"test_fail(lambda: raise_with_msg(Exception(\"The first message\"), msg, True), contains=\"This is my custom message!\")"
2134+
"\n",
2135+
"test_fail(lambda: (_ for _ in ()).throw(modify_exception(Exception(), None)), contains='')\n",
2136+
"test_fail(lambda: (_ for _ in ()).throw(modify_exception(Exception(), msg)), contains=msg)\n",
2137+
"test_fail(lambda: (_ for _ in ()).throw(modify_exception(Exception(\"The first message\"), msg)), contains=\"The first message This is my custom message!\")\n",
2138+
"test_fail(lambda: (_ for _ in ()).throw(modify_exception(Exception(\"The first message\"), msg, True)), contains=\"This is my custom message!\")"
21292139
]
21302140
},
21312141
{
@@ -2297,13 +2307,6 @@
22972307
"from nbdev.export import notebook2script\n",
22982308
"notebook2script()"
22992309
]
2300-
},
2301-
{
2302-
"cell_type": "code",
2303-
"execution_count": null,
2304-
"metadata": {},
2305-
"outputs": [],
2306-
"source": []
23072310
}
23082311
],
23092312
"metadata": {

0 commit comments

Comments
 (0)