Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app.on_exception and async #1370

Closed
kleynjan opened this issue Aug 8, 2023 · 3 comments
Closed

app.on_exception and async #1370

kleynjan opened this issue Aug 8, 2023 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@kleynjan
Copy link
Contributor

kleynjan commented Aug 8, 2023

Description

In #445, Falko gives a minimal usage example of app.on_exception, but it doesn't work with async:

from nicegui import app, ui

app.on_exception(ui.notify)

async def impossible():
    print(1 / 0)

ui.button("Do something impossible", on_click=impossible)

ui.run()

Apparently, the get_client() in globals fails:

Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 63, in uvloop.loop.Handle._run
  File "...lib/python3.11/site-packages/nicegui/background_tasks.py", line 59, in _handle_task_result
    globals.handle_exception(e)
  File ".../lib/python3.11/site-packages/nicegui/globals.py", line 114, in handle_exception
    result = handler() if not inspect.signature(handler).parameters else handler(exception)
                                                                         ^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/nicegui/functions/notify.py", line 35, in notify
    outbox.enqueue_message('notify', options, globals.get_client().id)
                                              ^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/nicegui/globals.py", line 101, in get_client
    return get_slot().parent.client
           ^^^^^^^^^^
  File ".../lib/python3.11/site-packages/nicegui/globals.py", line 97, in get_slot
    return get_slot_stack()[-1]
           ~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Going by the docs it's hard to say if this is a bug or a feature request. If the latter, as @groucho86 already suggested in #445, it would be nice to have a general mechanism to catch exceptions that works in async apps too. For now I am back to using the decorators that @smojef shared in #289.

Cheers, -Peter

@falkoschindler
Copy link
Contributor

Thanks for reporting this bug, @kleynjan!
It took some time to get to the bottom of this issue. But apparently we didn't handle the exception for async callbacks in the right place, so the UI context got lost. The ui.timer already did it correctly. It should be fix with commit 9a4082b.

@falkoschindler falkoschindler added the bug Something isn't working label Aug 10, 2023
@falkoschindler falkoschindler self-assigned this Aug 10, 2023
@falkoschindler falkoschindler added this to the 1.3.9 milestone Aug 10, 2023
@kleynjan
Copy link
Contributor Author

Thanks, Falko!

@rodja
Copy link
Member

rodja commented Aug 10, 2023

v1.3.9 has just been released!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants