Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def __init__(self, page: page, *, request: Request | None = None) -> None:
self.page = page
self.outbox = Outbox(self)

if self._request is not None:
self._request.scope['nicegui_page_path'] = self.page.path

with Element('q-layout', _client=self).props('view="hhh lpr fff"').classes('nicegui-layout') as self.layout:
with Element('q-page-container') as self.page_container:
with Element('q-page'):
Expand Down
2 changes: 2 additions & 0 deletions nicegui/nicegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ async def _exception_handler_404(request: Request, exception: Exception) -> Resp

@app.exception_handler(Exception)
async def _exception_handler_500(request: Request, exception: Exception) -> Response:
if not request.scope.get('nicegui_page_path'):
raise exception # Simply return "Internal Server Error", just like FastAPI would do
log.exception(exception)
with Client(page(''), request=request) as client:
error_content(500, exception)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ async def page():
screen.assert_py_logger('ERROR', 'some exception')


def test_api_exception(screen: Screen):
@app.get('/')
def api_exception():
raise RuntimeError('some exception in a GET endpoint')

screen.allowed_js_errors.append('/ - Failed to load resource')
screen.open('/')
screen.should_contain('Internal Server Error')


def test_page_with_args(screen: Screen):
@ui.page('/page/{id_}')
def page(id_: int):
Expand Down