Skip to content

Conversation

@evnchn
Copy link
Collaborator

@evnchn evnchn commented Nov 26, 2025

Motivation

Fix #5508, where NiceGUI returns the HTML for the "sad face error page" under this FastAPI GET request:

@app.get("/api_error")
def api():
    return {"response": 1/0}

Problems:

  • Wasteful in bandwidth
  • Difficult to parse and detect (remember: client_id is dynamic)
  • Non API-like behaviour

Optimally for @app, it should act as much like FastAPI as possible. This PR does exactly that.

Implementation

  • Mark NiceGUI page requests in request scope
  • Only return client.build_response(request, 500) if found
  • If not we raise exception and FastAPI proceeds to "Internal Server Error" exactly like if a plain FastAPI app would.

Results

image

Progress

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

@evnchn evnchn added the bug Type/scope: Incorrect behavior in existing functionality label Nov 26, 2025
@falkoschindler falkoschindler requested a review from rodja November 26, 2025 10:38
@falkoschindler falkoschindler added the review Status: PR is open and needs review label Nov 26, 2025
@falkoschindler falkoschindler added this to the 3.5 milestone Nov 26, 2025
@falkoschindler falkoschindler modified the milestones: 3.5, 3.6 Jan 6, 2026
@falkoschindler falkoschindler requested review from falkoschindler and removed request for rodja January 14, 2026 14:14
Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @evnchn!
Looks good to me. 😊

Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I approved too early. I found an issue while the CI was running:

We are logging twice for raw API requests.

  • Should we move log.exception(exception) below the early exit? Otherwise we log twice for exceptions in raw API endpoints.

  • Alternatively we could return a response rather than raising:

    return PlainTextResponse('Internal Server Error', status_code=500)

    This way the _exception_handler_500 always logs and - depending on the scope - returns a plain text or page response, without raising and letting Starlette handle the problem intransparently.

What do you think?

Copy link
Collaborator Author

@evnchn evnchn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix things. We do not mock the Internal Server Error because one may customize the FastAPI error handling.

@evnchn
Copy link
Collaborator Author

evnchn commented Jan 15, 2026

And if I am not mistaken, the native FastAPI behavior is to not log. Not nice to the developer, but again, it's not called NiceAPI.

@falkoschindler falkoschindler added this pull request to the merge queue Jan 15, 2026
Merged via the queue into zauberzeug:main with commit e874121 Jan 15, 2026
7 checks passed
@evnchn evnchn deleted the raw-error-for-api-exceptions branch January 15, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Type/scope: Incorrect behavior in existing functionality review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error page should only apply to ui.page endpoints

2 participants