Skip to content

feature request: Support rendering for exception handlers #62

@JacobHayes

Description

@JacobHayes

I've been using a 404 exception handler like below:

    @app.exception_handler(status.HTTP_404_NOT_FOUND)
    async def _(request: Request, exc: HTTPException) -> HTMLResponse:
        return templates.TemplateResponse(
            request=request, name="404.html", context={"...": ...}
        )

It'd be helpful if there was a way to use fasthx with exception handlers too, for example an exception: bool flag to .hx and .page. When converting my other routes to use fasthx, I would otherwise not reference fastapi's TemplateResponse anymore.


If you try to use fasthx for this now:

    @app.exception_handler(status.HTTP_404_NOT_FOUND)
    @jinja.page("404.html")
    async def _(request: Request, exc: HTTPException) -> HTMLResponse:
        return {"...": ...}

it raises an error like below:

File "<...>/.venv/lib/python3.13/site-packages/fasthx/utils.py", line 46, in execute_maybe_sync_func
    return await func(*args, **kwargs)  # type: ignore[no-any-return]
                 ~~~~^^^^^^^^^^^^^^^^^
TypeError: create_app.<locals>._() missing 1 required positional argument: 'exc'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions