nicegui icon indicating copy to clipboard operation
nicegui copied to clipboard

Provide global exception handler

Open falkoschindler opened this issue 3 years ago • 1 comments

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.

falkoschindler avatar Feb 27 '23 14:02 falkoschindler

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.

smojef avatar Feb 27 '23 15:02 smojef

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.

falkoschindler avatar Mar 15 '23 21:03 falkoschindler

Great! You are absolutely right. The app.on_exception is the most important. I can't wait to try it. Thanks,

smojef avatar Mar 15 '23 21:03 smojef

Can someone please write up a small sample where ui.notify is used to show a raised Exception?

groucho86 avatar May 08 '23 14:05 groucho86

@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 avatar May 08 '23 20:05 falkoschindler

@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.

groucho86 avatar May 09 '23 16:05 groucho86