|
1542 | 1542 | "output_type": "stream",
|
1543 | 1543 | "text": [
|
1544 | 1544 | "Traceback (most recent call last):\n",
|
1545 |
| - " File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/2963369439.py\", line 14, in python\n", |
| 1545 | + " File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/2963369439.py\", line 14, in python\n", |
1546 | 1546 | " try: return _run(code, glb, loc)\n",
|
1547 | 1547 | " ^^^^^^^^^^^^^^^^^^^^\n",
|
1548 |
| - " File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/1858893181.py\", line 18, in _run\n", |
| 1548 | + " File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/1858893181.py\", line 18, in _run\n", |
1549 | 1549 | " try: exec(compiled_code, glb, loc)\n",
|
1550 | 1550 | " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
1551 | 1551 | " File \"<ast>\", line 1, in <module>\n",
|
1552 |
| - " File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/2963369439.py\", line 9, in handler\n", |
| 1552 | + " File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/2963369439.py\", line 9, in handler\n", |
1553 | 1553 | " def handler(*args): raise TimeoutError()\n",
|
1554 | 1554 | " ^^^^^^^^^^^^^^^^^^^^\n",
|
1555 | 1555 | "TimeoutError\n",
|
|
1841 | 1841 | " \"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n",
|
1842 | 1842 | " if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n",
|
1843 | 1843 | " func = ns[fc_name]\n",
|
| 1844 | + " # Clean up bad param names\n", |
| 1845 | + " inps = {re.sub(r'\\W', '', k):v for k,v in fc_inputs.items()}\n", |
1844 | 1846 | " try: return func(**fc_inputs)\n",
|
1845 | 1847 | " except Exception as e:\n",
|
1846 |
| - " if raise_on_err: raise e\n", |
| 1848 | + " if raise_on_err: raise e from None\n", |
1847 | 1849 | " else: return traceback.format_exc()"
|
1848 | 1850 | ]
|
1849 | 1851 | },
|
|
1917 | 1919 | "test_fail(call_func, args=['subs', {'a': 1, 'b': '3'}], kwargs={'ns': mk_ns(d)})"
|
1918 | 1920 | ]
|
1919 | 1921 | },
|
1920 |
| - { |
1921 |
| - "cell_type": "code", |
1922 |
| - "execution_count": null, |
1923 |
| - "id": "b19298ac", |
1924 |
| - "metadata": {}, |
1925 |
| - "outputs": [], |
1926 |
| - "source": [ |
1927 |
| - "%%ai\n", |
1928 |
| - "How do I get the whole traceback of an error instead of just str(e) like above?" |
1929 |
| - ] |
1930 |
| - }, |
1931 |
| - { |
1932 |
| - "cell_type": "markdown", |
1933 |
| - "id": "6ec89b42", |
1934 |
| - "metadata": {}, |
1935 |
| - "source": [ |
1936 |
| - "To get the whole traceback of an error instead of just `str(e)`, you can use the `traceback` module, which you've already imported in your code. Modify the `call_func` function to capture and return the full traceback when an error occurs:\n", |
1937 |
| - "\n", |
1938 |
| - "```python\n", |
1939 |
| - "#| exports\n", |
1940 |
| - "def call_func(fc_name, fc_inputs, ns, raise_on_err=True):\n", |
1941 |
| - " \"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n", |
1942 |
| - " if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n", |
1943 |
| - " func = ns[fc_name]\n", |
1944 |
| - " try: return func(**fc_inputs)\n", |
1945 |
| - " except Exception as e:\n", |
1946 |
| - " if raise_on_err: raise e\n", |
1947 |
| - " else: return traceback.format_exc()\n", |
1948 |
| - "```\n", |
1949 |
| - "\n", |
1950 |
| - "This replaces `str(e)` with `traceback.format_exc()`, which returns the full traceback as a string, including the error type, message, and the call stack that led to the error. This gives you much more context about where and why the error occurred." |
1951 |
| - ] |
1952 |
| - }, |
1953 | 1922 | {
|
1954 | 1923 | "cell_type": "markdown",
|
1955 | 1924 | "id": "591574b8-6b53-4908-8159-b87be42133f7",
|
|
2002 | 1971 | " if inspect.iscoroutine(res):\n",
|
2003 | 1972 | " try: res = await res\n",
|
2004 | 1973 | " except Exception as e:\n",
|
2005 |
| - " if raise_on_err: raise e\n", |
| 1974 | + " if raise_on_err: raise e from None\n", |
2006 | 1975 | " else: return traceback.format_exc()\n",
|
2007 | 1976 | " return res"
|
2008 | 1977 | ]
|
|
2035 | 2004 | "metadata": {},
|
2036 | 2005 | "outputs": [],
|
2037 | 2006 | "source": [
|
2038 |
| - "test_eq(await call_func_async('asums', {'a': 1, 'b': '2'}, ns=[asums], raise_on_err=False), \"unsupported operand type(s) for +: 'int' and 'str'\")" |
| 2007 | + "r = await call_func_async('asums', {'a': 1, 'b': '2'}, ns=[asums], raise_on_err=False)\n", |
| 2008 | + "assert \"unsupported operand type(s) for +: 'int' and 'str'\" in r" |
2039 | 2009 | ]
|
2040 | 2010 | },
|
2041 | 2011 | {
|
|
0 commit comments