Is there a way to redirect exceptions to frontend text box?

When I have:

Exception in callback functools.partial(<bound method IOLoop._discard_future_result

Is there a way to stream these exceptions to something like pn.pane.Markdown()?

Something could be built of the below

import panel as pn
import time
import traceback

error_panel = pn.pane.Markdown(height=200, width=800)
button = pn.widgets.Button(name="Run")

def raise_error(*events):
    time.sleep(0.2)
    try:
        raise ValueError("Error. No error Provided")
    except Exception as ex:
        error_panel.object = "```bash\n" + traceback.format_exc() + "\n```"

button.on_click(raise_error)
pn.Column(error_panel, button).servable()

Thanks for the quick reply!

I was wondering if there was a more general way rather than putting try/except in every function?

I don’t think so. But eventually there should be some mechanism. Maybe an idea for a Feature Request?

2 Likes

Thanks. I’ve decided to use decorators to wrap try/except.


    def try_decorator(function):
        @functools.wraps(function)
        def wrapper(self, *args, **kwargs):
            try:
                return function(self, *args, **kwargs)
            except Exception as e:
                self.log_box.value = str(e)
        return wrapper

@try_decorator
def func()...
2 Likes

having a standard way, certainly in the default templates, for all uncaught exceptions to be presented to the user, such as Grafana does when an error occurs in one of its panels, would certainly be something I think should be there.

Has anyone put in a feature request?

3 Likes

I don’t think so.

1 Like

There’s a reference to this feature here: python - Raise errors and display traceback messages from panel dashboards - Stack Overflow