Informing user of callback exceptions

Hi,

Since Dash 2.17.0 I think this is possible with set_props.

I attach a decorator to Dash.callback that catches any exceptions (except PreventUpdate) and uses set_props to show the user error info (a dbc Modal in my case). I think it’s needed for set_props to work to return dash.no_update rather than re-raise the error. To return the right number of dash.no_update’s I do some python argument processing:

def error_handle_callback(outputs: int):
        def decorator(func):
            def wrapper(*args, **error_handle_kwargs):
                try:
                    result = func(*args, **error_handle_kwargs)
                except Exception as e:
                    if isinstance(e, PreventUpdate):
                        raise PreventUpdate
                    set_props('error-modal', {'is_open': True})
                    error_traceback = exception_str()
                    set_props('callback-error', {'children': error_traceback})
                    return tuple(dash.no_update for _ in range(outputs))
                return result
            return wrapper
        return decorator

    callback = dash_app.callback

    def new_callback(*args, **decorator_kwargs):
        def decorator(func):
            new_func = error_handle_callback(outputs=sum([isinstance(arg, Output) for arg in args]))(func)
            return callback(*args, **decorator_kwargs)(new_func)
        return decorator

    dash_app.callback = new_callback

# ensure to create all callbacks after this point
1 Like