Skip to content

Conversation

rodja
Copy link
Member

@rodja rodja commented Sep 14, 2025

Motivation

While working on #5116, I saw some strange behaviours and inconsistencies when pages raise exceptions. Here are some examples:

  • check_for_late_return_value simply accessed task.result() which itself raises an exception... but that complicates the stack trace
  • create_error_page simply raised e if no page exception handler was registered -- leading to complicated stacktrace
  • wait_for_result always created an error page response on exception; even when the response was already send to the browser
  • if not client.sub_pages_router._can_resolve_full_path(client), a error page was returned and not a response

Implementation

NOTE: this PR is based on #5116 to avoid merge conflicts -- ideally we can merge #5116 to 3.0 before this one and then change target branch

What has been done so far:

shorter stacktrace for page builders

Consider raising an exception after client.connected():

from nicegui import ui

async def root():
    await context.client.connected()
    raise Exception('test')

ui.run(root)

Before this PR the stacktrace is

Traceback (most recent call last):
  File "/Users/rodja/Projects/nicegui/nicegui/page.py", line 130, in check_for_late_return_value
    if task.result() is not None:
       ^^^^^^^^^^^^^
  File "/Users/rodja/Projects/nicegui/nicegui/page.py", line 184, in wait_for_result
    return await result
           ^^^^^^^^^^^^
  File "/Users/rodja/Projects/nicegui/test.py", line 106, in index
    raise Exception('test')

By checking for task.cancelled() and task.exception() in check_for_late_return_value its now a bit cleaner:

observed exception
Traceback (most recent call last):
  File "/Users/rodja/Projects/nicegui/nicegui/page.py", line 170, in wait_for_result
    return await result
           ^^^^^^^^^^^^
  File "/Users/rodja/Projects/nicegui/test.py", line 106, in index
    raise Exception('test')

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
  • Pytests have been added (or are not necessary).
  • Documentation has been added (or is not necessary).

@rodja
Copy link
Member Author

rodja commented Sep 14, 2025

Still seven tests are failing...

FAILED tests/test_page.py::test_exception - IndexError: pop from empty list
FAILED tests/test_select.py::test_add_new_values[add-False-True] - IndexError: pop from empty list
FAILED tests/test_select.py::test_add_new_values[add-True-True] - IndexError: pop from empty list
FAILED tests/test_storage.py::test_missing_storage_secret - IndexError: pop from empty list
FAILED tests/test_sub_pages.py::test_http_404_on_initial_request - AssertionError: assert 500 == 404
FAILED tests/test_sub_pages.py::test_http_404_on_initial_request_with_async_page_builder - AssertionError: assert 500 == 404
FAILED tests/test_sub_pages.py::test_http_404_on_initial_request_with_async_sub_page_builder - AssertionError: assert 500 == 404
ERROR tests/test_scene.py::test_moving_sphere_with_timer - Failed: There were unexpected ERROR logs.

@evnchn
Copy link
Collaborator

evnchn commented Sep 14, 2025

wait_for_result always created an error page response on exception; even when the response was already send to the browser

Somewhere along the commit history of #4538 there's provision for sending the error to the browser client in the case that the page was returned to the browser, then it errored out, addressing the above by adding a new function.

It may be helpful if you take example of that, though to be honest that is a pretty rough PR in the beginning so your mileage may vary.

@evnchn
Copy link
Collaborator

evnchn commented Sep 14, 2025

3a5dbf3

Base automatically changed from timeout-errors-are-ok to 3.0 September 24, 2025 14:34
Base automatically changed from 3.0 to main October 3, 2025 15:14
@evnchn
Copy link
Collaborator

evnchn commented Oct 6, 2025

#5116 merged, so we close this one? Or there are still stuff of value in this PR? (on mobile can't check)

@rodja
Copy link
Member Author

rodja commented Oct 7, 2025

I think there are still valuable parts in this PR. But I've been to busy to look at it. Let's keep it open for a while longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants