Debian Codegraph @

docs/source/cookbook/rich_formatted_exceptions.rst

raw
1 =========================
2 Rich Formatted Exceptions
3 =========================
4 Tracebacks of uncaught exceptions provide valuable feedback for debugging. This guide demonstrates how to enhance your error messages using rich formatting.
5
6 -------------------------
7 Standard Python Traceback
8 -------------------------
9 Consider the following example:
10
11 .. code-block:: python
12
13 from cyclopts import App
14
15 app = App()
16
17 @app.default
18 def main(name: str):
19 print(name + 3)
20
21 if __name__ == "__main__":
22 app()
23
24 Running this script will produce a standard Python traceback:
25
26 .. code-block:: console
27
28 $ python my-script.py foo
29 Traceback (most recent call last):
30 File "/cyclopts/my-script.py", line 12, in <module>
31 app()
32 File "/cyclopts/cyclopts/core.py", line 903, in __call__
33 return command(*bound.args, **bound.kwargs)
34 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35 File "/cyclopts/my-script.py", line 8, in main
36 print(name + 3)
37 ~~~~~^~~
38 TypeError: can only concatenate str (not "int") to str
39
40 ------------------------
41 Rich Formatted Traceback
42 ------------------------
43 To create a more visually appealing and informative traceback, you can use the `Rich library's traceback handler`_. Here's how to modify your script:
44
45 .. code-block:: python
46
47 import sys
48 from cyclopts import App
49 from rich.console import Console
50 from rich.traceback import install as install_rich_traceback
51
52 error_console = Console(stderr=True)
53 app = App(console=console, error_console=error_console)
54
55 # Install rich traceback handler using the error console
56 install_rich_traceback(console=error_console)
57
58 @app.default
59 def main(name: str):
60 print(name + 3)
61
62 if __name__ == "__main__":
63 app()
64
65 Now, running the updated script will display a rich-formatted traceback:
66
67 .. code-block:: console
68
69 $ python my-script.py foo
70 ╭──────────────── Traceback (most recent call last) ─────────────────╮
71 │ /cyclopts/my-script.py:16 in <module> │
72 │ │
73 │ 13 │
74 │ 14 if __name__ == "__main__": │
75 │ 15 │ try: │
76 │ ❱ 16 │ │ app() │
77 │ 17 │ except Exception: │
78 │ 18 │ │ console.print_exception(width=70) │
79 │ 19 │
80 │ │
81 │ /cyclopts/cyclopts/core.py:903 in __call__ │
82 │ │
83 │ 900 │ │ │ │ │
84 │ 901 │ │ │ │ return asyncio.run(command(*bound.args, **b │
85 │ 902 │ │ │ else: │
86 │ ❱ 903 │ │ │ │ return command(*bound.args, **bound.kwargs) │
87 │ 904 │ │ except Exception as e: │
88 │ 905 │ │ │ try: │
89 │ 906 │ │ │ │ from pydantic import ValidationError as Pyd │
90 │ │
91 │ /cyclopts/my-script.py:11 in main │
92 │ │
93 │ 8 │
94 │ 9 @app.default │
95 │ 10 def main(name: str): │
96 │ ❱ 11 │ print(name + 3) │
97 │ 12 │
98 │ 13 │
99 │ 14 if __name__ == "__main__": │
100 ╰────────────────────────────────────────────────────────────────────╯
101
102 This rich-formatted traceback provides a more readable and visually appealing representation of the error, but may make copy/pasting for sharing a bit more cumbersome.
103
104 .. _Rich library's traceback handler: https://rich.readthedocs.io/en/stable/traceback.html#printing-tracebacks