Code Monkey home page Code Monkey logo

Comments (2)

volfpeter avatar volfpeter commented on July 24, 2024

Hi,

I'd recommend implementing an exception handler middleware that converts exceptions to HTML responses. The middleware has access to the request, it can check whether it was an hx-request or not if it matters for your use-case.


FastAPI must resolve dependencies (and validate the route's data inputs) before calling the route, so if the API's input is incorrect, FastAPI will raise an HTTPException with 422 status code before calling the route (and its fasthx wrapper). As the fasthx decorator is not executed, it can not solve such an issue.

Regarding the response code: if your route returns a Response object, FastHX decorators will simply return it without doing anything. If your route doesn't return a Response object and you define status codes in the FastAPI path operation decorator, that's out of of reach for FastHX, so it can't do anything.


I assume you only want to differentiate between a successful response and error cases. If the returned error HTML is fairly generic (part of its content could come from HTTPException or a custom subclass with extra properties), then a middleware would be a good solution for you. This lib could implement a generic, ready to use middleware, but I'd need more user feedback (and some consensus) on how it should work. This could also be an APIRouter-specific thing, then again, a specialized APIRouter subclass could be added to the lib, but without input from several users on how it should work, I'm hesitant to add such a feature. And finally, as outlined above, a route-specific decorator couldn't handle input errors, because the request doesn't reach the decorator, so it can not be a complete solution on its own.

Peter

from fasthx.

volfpeter avatar volfpeter commented on July 24, 2024

Closing for lack of response.

from fasthx.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.