Comments (2)
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.
Closing for lack of response.
from fasthx.
Related Issues (10)
- Add more examples HOT 4
- Pass endpoint dependencies to Jinja context HOT 7
- Swagger compliant `HX-Request` headers. HOT 6
- Unable to access Response in the Route HOT 10
- Allow non-dict return values by default for Jinja template rendering HOT 1
- Run sync routes in a threadpool HOT 1
- Add a page() method to Jinja which renders the template even for non-HTMX requests HOT 1
- [Jinja] Let routes choose from multiple templates
- Returning HX-* headers with response HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fasthx.