Comments (2)
Hi @abhishekc-sharma -
Thanks for the thoughtful note, and for applying to Cloudflare's internship program! One of our interns @leoorshansky helped a TON on this project, maybe you can too :)
What would be the idiomatic way to implement Error Handling in my worker ? My understanding of how it is setup currently is that any error from my handler would be converted to a worker::Error which by default causes an error response of some sort with an HTML body. If I were developing a REST API with JSON responses what would be the cleanest way to convert my errors to the appropriate JSON responses with a unique error code ?
We should probably re-visit the decision to use Result
types around the response returned from each handler. This was done to make some of the code more useful when writing the #[event]
macros, but maybe time to take another look.
To answer your question though, I would write a utility function that returns Result<Response>
and takes some message for you to re-use. Maybe to be mapped onto existing Result<Response>
returned from places you might call Response::from_json
etc.
I would be happy to consider any suggestions you have here! It's hard to cover everything when designing a framework like this -- and knowing when not to add features is just as important. We want to be generally useful, without imposing too many patterns on users who have varying use-cases.
Maybe related to the above - there doesn't seem to be any support for adding middleware to my Router. This proved troublesome when I had to add CORS headers to all my responses or to selectively authenticate some of my handlers. Is the expectation to use one of the existing crates in the ecosystem to handle this ? Or is it just something thats on the roadmap.
Yea, this is a great point. It's definitely something we should include support for. If you have any ideas on how you'd like to use it, it would be helpful to start with some design ideas.
I would think that this would likely need to be done at the Router
level before you register any handlers, a la
let mut router = Router::new();
router.add(check_auth);
router.add(cors);
...
where check_auth
and cors
are fn pointers that are called with the same Request
and Env
arguments and return Result<Response>
or Response
directly.
Please feel free to leave notes / ideas here, and join us on Discord: https://discord.com/invite/cloudflaredev in the #workers-rs
channel.
from workers-rs.
Thanks a lot for you detailed response @nilslice .
I agree, it would be hard to specialize the library for every possible potential use case. Having to write my own wrapper is probably fair enough here.
Yeah, I was thinking of a router API something along those lines. I'm most familiar with the Router interface of the Go library Gorilla. Have to explore further to see how the other backend frameworks in Rust do the same thing.
from workers-rs.
Related Issues (20)
- [Feature] Make `chrono-tz` an optional dependency HOT 8
- Improve top-level error handling HOT 3
- [BUG] `http` feature is not named accurately. HOT 1
- [BUG] `worker-kv` is not released together with other crates HOT 3
- [Feature] Add #[worker::trait_send] for async_trait
- π€ΎββοΈ [Feature] Most interop methods should be marked with `catch` HOT 4
- worker-kv does not serialize struct metadata HOT 3
- [BUG] CORS Access-Control-Allow-Origin set wrong if using multiple origins
- [BUG] Range requests in R2 are limited to 32 bit offset/length HOT 1
- [Feature] Make ObjectNamespace Send + Sync
- [BUG] No way to return pre-compressed data with Content-Encoding from worker-rs due to missing encodeBody option
- [BUG] templates/axum build error
- [Feature] Some files should be ignored in worker project
- `import shim from ". /shim.mjs"` worked fine on cloudflare pages a couple weeks ago, now I get `TypeError: an.fetch is not a function` HOT 9
- [BUG] `Response.json()` checks Content-Type header
- [Feature] Durable Objects do not respect the http feature
- [BUG] new wasm-pack version v0.13.0 breaking build HOT 3
- [BUG] POST request not working with `Fetch::Request` HOT 1
- [Feature] Access bundled files HOT 4
- [BUG] The argument '--out-dir <OUT_DIR>' cannot be used multiple timesβ error in wasm-pack when specifying --out-dir for worker-build. HOT 1
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 workers-rs.