Custom Error Pages #883
Replies: 3 comments 1 reply
-
Thanks. I like the idea. An alternative to passing a builder to @ui.error_page(404)
def page_not_found():
ui.label('Sorry, this page does not exist') |
Beta Was this translation helpful? Give feedback.
-
You can already overwrite the error handling of NiceGUI by doing something like this: from nicegui import ui, Client
from nicegui.page import page
@app.exception_handler(404)
async def exception_handler_404(request: Request, exception: Exception) -> Response:
with Client(page('')) as client:
ui.label('Sorry, this page does not exist')
return client.build_response(request, 404) |
Beta Was this translation helpful? Give feedback.
-
For those trying this at home in 2024, note that to create the Client object you now have to pass the request object: from nicegui import ui, Client
from nicegui.page import page
@app.exception_handler(404)
async def exception_handler_404(request: Request, exception: Exception) -> Response:
with Client(page(''), request=request) as client:
ui.label('Sorry, this page does not exist')
return client.build_response(request, 404) My 500 handler is based on this -- which thanks to theme.py (see modularization example) keeps the user nicely in the app context rather than "throw them out" of the app, while sending the stack trace to support: @app.exception_handler(500)
async def exception_handler_500(request: Request, exception: Exception) -> Response:
stack_trace = traceback.format_exc()
msg_to_user = f"**{exception}**\n\nStack trace: \n<pre>{stack_trace}"
if OTAP == 'P': # production -> send trace by mail & nice soothing message
mailres = send_mail_somehow(from, to, etc, etc, msg_to_user)
msg_to_user = _("Incident has been reported, repair is underway.")
with Client(page(''), request=request) as client:
with theme.frame("error"):
with ui.card().classes("error-card"):
ui.label(_("500 Application Error")).classes("heading")
ui.markdown(msg_to_user).classes("message")
return client.build_response(request, 500) |
Beta Was this translation helpful? Give feedback.
-
I may be missing a way to do this, but as far as I can see error pages are generated from
nicegui.error.error_content
I would like the ability to fully customize the error page, or entirely overwrite the error_content function.
I see this as the error function being converted into a class, allowing inheritance and modification. A new argument could be added ui.run specifying the new class to generate the error pages.
Beta Was this translation helpful? Give feedback.
All reactions