๐ก๏ธย ย Exception Handler
Submitted by Arnaud Miribel
Summary
Override Streamlit's uncaught exception handler to customize error display and logging.
Functions
set_global_exception_handler
Replace Streamlit's global uncaught exception handler with f.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
New exception handler function. |
required |
Notes
- For Streamlit versions earlier than 1.39.0, the handler lives under
streamlit.runtime.scriptrunner.script_runner. - For Streamlit 1.39.0 and later, it resides under
streamlit.error_util.
Warning
This function mutates Streamlit internals. Use carefully and test thoroughly.
Source code in src/streamlit_extras/exception_handler/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example() -> None:
"""Demonstrate installing a custom handler and triggering an exception.
Includes a sample handler that logs context and still surfaces the exception to the user.
Raises:
RuntimeError: A demo exception to demonstrate the custom handler.
"""
st.write("Install a custom handler that logs context and shows the exception to the user.")
if st.button("Install custom handler"):
set_global_exception_handler(_custom_exception_handler)
st.success("Custom handler installed for this app run.")
if st.button("Trigger an exception"):
raise RuntimeError("Boom! This is a demo exception.")