|
11 | 11 | }, |
12 | 12 | { |
13 | 13 | "cell_type": "code", |
14 | | - "execution_count": 2, |
| 14 | + "execution_count": 1, |
15 | 15 | "metadata": {}, |
16 | 16 | "outputs": [], |
17 | 17 | "source": [ |
|
21 | 21 | }, |
22 | 22 | { |
23 | 23 | "cell_type": "code", |
24 | | - "execution_count": 3, |
| 24 | + "execution_count": 2, |
25 | 25 | "metadata": {}, |
26 | 26 | "outputs": [], |
27 | 27 | "source": [ |
|
44 | 44 | }, |
45 | 45 | { |
46 | 46 | "cell_type": "code", |
47 | | - "execution_count": 4, |
| 47 | + "execution_count": 3, |
48 | 48 | "metadata": {}, |
49 | 49 | "outputs": [], |
50 | 50 | "source": [ |
|
2100 | 2100 | }, |
2101 | 2101 | { |
2102 | 2102 | "cell_type": "code", |
2103 | | - "execution_count": 133, |
| 2103 | + "execution_count": null, |
2104 | 2104 | "metadata": {}, |
2105 | 2105 | "outputs": [], |
| 2106 | + "source": [] |
| 2107 | + }, |
| 2108 | + { |
| 2109 | + "cell_type": "code", |
| 2110 | + "execution_count": 158, |
| 2111 | + "metadata": { |
| 2112 | + "tags": [] |
| 2113 | + }, |
| 2114 | + "outputs": [], |
2106 | 2115 | "source": [ |
2107 | 2116 | "#|export\n", |
2108 | | - "def raise_with_msg(\n", |
| 2117 | + "def modify_exception(\n", |
2109 | 2118 | " 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" |
2116 | 2125 | ] |
2117 | 2126 | }, |
2118 | 2127 | { |
2119 | 2128 | "cell_type": "code", |
2120 | | - "execution_count": 132, |
| 2129 | + "execution_count": 164, |
2121 | 2130 | "metadata": {}, |
2122 | 2131 | "outputs": [], |
2123 | 2132 | "source": [ |
2124 | 2133 | "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!\")" |
2129 | 2139 | ] |
2130 | 2140 | }, |
2131 | 2141 | { |
|
2297 | 2307 | "from nbdev.export import notebook2script\n", |
2298 | 2308 | "notebook2script()" |
2299 | 2309 | ] |
2300 | | - }, |
2301 | | - { |
2302 | | - "cell_type": "code", |
2303 | | - "execution_count": null, |
2304 | | - "metadata": {}, |
2305 | | - "outputs": [], |
2306 | | - "source": [] |
2307 | 2310 | } |
2308 | 2311 | ], |
2309 | 2312 | "metadata": { |
|
0 commit comments