Provide global exception handler
In #289 we got the idea of providing an app.on_exception event which can be used for registering a custom exception handler, either for showing notifications, logging to a file or whatever.
Ideally, users would want an easy way of displaying exceptions. Some ideas:
- A simple one-liner to show exceptions, like
app.on_exception(ui.exceptions_dialog). - Maybe this behavior should even be the default, and we could revert to the previous behavior with
app.on_exception(None). - Further ideas about
ui.exceptions_dialog:- It could display the exception with meaningful info, like an expansible stack trace in
<pre>. - It could handle fast repeating exceptions without filling the screen (unlike
ui.notify). - It could even be a small warning icon popup, that when clicked shows the dialog exceptions details.
- It could even mention "Developers can hide exceptions with
app.on_exception(None)" for transition if required. - Looking at its source, users could customize and create their own dialog/handler if required.
- It could display the exception with meaningful info, like an expansible stack trace in
I just implemented the app.on_exception method for registering exception handlers. By default there is only globals.log.exception registered, but you can add print, ui.notify, or something completely custom.
Regarding ui.exceptions_dialog: In my point of view this feature is not yet very clear and it might be hard to find an implementation/layout/design that suits everyones needs. Consider an optional stack trace, throttling, styling, light/dark theme, desktop/mobile, translation, ... But I'm open for suggestions. I just think app.on_exception is a very useful feature by itself and should be released independent of a potential ui.exceptions_dialog.
Great! You are absolutely right. The app.on_exception is the most important. I can't wait to try it. Thanks,
Can someone please write up a small sample where ui.notify is used to show a raised Exception?
@groucho86
from nicegui import app, ui
app.on_exception(ui.notify)
ui.button('Do something impossible', on_click=lambda: print(1 / 0))
ui.run()
@falkoschindler thank you! That works beautifully, but I'm struggling with utilizing it with asyncio.
It could be useful to show error handling in some of the examples, eg. ffmpeg_extract_images, script_executor, etc.