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

[BUG] Exception Handlers Return 200 Status Code Instead of Original Error Code #633

Closed
4 tasks done
tom-pollak opened this issue Jan 11, 2025 · 1 comment
Closed
4 tasks done
Labels
bug Something isn't working

Comments

@tom-pollak
Copy link
Contributor

tom-pollak commented Jan 11, 2025

@jph00

When using custom exception handlers, the response status code defaults to 200 instead of preserving the original error code. This occurs unless explicitly returning a HTMLResponse with the correct status code.

This can be worked around by explicitly returning a Response with the correct status code, but maybe documented?

For example:

def _not_found(req, exc): return Titled('Oh no!', Div('We could not find that page :('))

Related: #460

Minimal example:

# %%
import requests
from fasthtml.common import *
from fasthtml.jupyter import JupyUvi

# problem
def _not_found(req, exc):
    print("(404 hit)")
    return Titled("404", "Page not found")

# current workaround -- to_xml required to convert to html string
def _unauthorized(req, exc):
    print("(401 hit)")
    return HTMLResponse(to_xml(Titled("401", "Unauthorized")), status_code=401)

app, rt = fast_app(exception_handlers={404: _not_found, 401: _unauthorized})

@rt()
def admin(req):
    print("(admin hit)")
    raise HTTPException(status_code=401, detail="Unauthorized")

@rt
def bad_request(req):
    print("(bad request hit)")
    raise HTTPException(status_code=400, detail="Bad Request")

server = JupyUvi(app)

# %%

# shows the error, by default the exception handler returns a 200 rather than 404
res_404 = requests.get("http://localhost:8000/not_found")
print(f"Response 404 handler (Titled): status_code={res_404.status_code}\n")

# a workaround for this, return a HTMLResponse directly
res_401 = requests.get("http://localhost:8000/admin")
print(f"Response 401 handler (manually set status code): status_code={res_401.status_code}\n")

# no exception handler for 400
res_400 = requests.get("http://localhost:8000/bad_request")
print(f"Response 400 (No handler): status_code={res_400.status_code}\n")

Output:

(404 hit)
Response 404 handler (Titled): status_code=200

(admin hit)
(401 hit)
Response 401 handler (Manually set status code): status_code=401

(bad request hit)
Response 400 (No handler): status_code=400

Environment Information

  • fastlite: 0.1.1
  • fastcore: 1.7.27
  • fasthtml: 0.12.0

Confirmation
Please confirm the following:

  • I have read the FAQ (https://docs.fastht.ml/explains/faq.html)
  • I have provided a minimal reproducible example
  • I have included the versions of fastlite, fastcore, and fasthtml
  • I understand that this is a volunteer open source project with no commercial support.
@tom-pollak tom-pollak added the bug Something isn't working label Jan 11, 2025
@tom-pollak tom-pollak mentioned this issue Jan 12, 2025
8 tasks
@tom-pollak
Copy link
Contributor Author

PR in #636

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

1 participant