Better documentation/discussion for error handling #31
Replies: 4 comments 6 replies
-
Thanks! The absence of error handling is not entirely a mistake, but I should address it directly in the documentation. Currently, the error handling story is the least-developed aspect of Trillium, but we are limited by what the language can express at the moment. In the future, we'll be able to have a trillium_smol::run(ErrorHandler::new(async move |conn: &Conn| -> Result<(), MyError> {
Ok(())
})); … but until the language develops the capability to express the bounds for There are two macros available to make this easier: trillium::conn_try! and trillium::conn_unwrap!, but I expect there'll be further improvements. Regardless of how it turns out, error handling is going to end up looking very different from tide. In particular, I think there's a distinction between "returning a non-200 response" and a rust Err, and that the code to translate between them should live entirely in the application. I think most applications will want to have a single thiserror-style enum Error that also implements Handler. |
Beta Was this translation helpful? Give feedback.
-
I was actually looking into some sort of error handling sort of middleware (handler) that auto converts errors to json/html bad requests or 500 or trillium_smol::run(
Router::new()
.get("/users", {conn-> Err('blah') ))
.get("/posts", posts_api),
ErrorHandler::new(async move |conn: &Conn| -> {
conn.with_status(500).with_body(json!(conn.err()))
});
); |
Beta Was this translation helpful? Give feedback.
-
One question I have regarding error handliing is that if possible I would want to add something like |
Beta Was this translation helpful? Give feedback.
-
I see that you have error-handling branch. Curious if you have plans to release it? Might be with macro. Might be macros with support for chaining #[handler]
async fn hello_world(ctx: &mut ctx) -> Result<(), MyError> {
ctx.with_status(200).with_body("Hello World");
Ok(())
} |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to add error handling example in https://trillium.rs/ ?
Doc are great by the way for a new project.
Beta Was this translation helpful? Give feedback.
All reactions