-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow returning custom error types in Result
#92
Comments
Thanks for the feedback. It's not currently supported, an async function should return But we can make some adjustments:
impl From<MyError> for Error {
fn from(value: MyError) -> Self {
Error::Responder(value.into_response())
}
}
async fn index(_: Request) -> Result<&'static str> {
Err(MyError {
message: "my error".to_string(),
})?;
Ok("Hello Viz")
}
async fn index_wrapped(req: Request) -> Result<&'static str> {
Ok(index(req).await?)
}
let app = Router::new().get("/", index_wrapped);
poc let app = Router::new().get("/", index.map_custom_error(IntoResponse::into_response)); |
Yes that is indeed a possible solution // custom data structure representing the error body
#[derive(Debug, thiserror::Error)]
pub struct MyError {
message: String,
}
impl From<viz::Error> for MyError {
fn from(value: viz::Error) -> Self {
// return whatever makes sense for viz::Error
Self {
message: "Something went wrong. Please try again later".into()
}
}
}
impl From<viz::PayloadError> for MyError {
fn from(value: viz::PayloadError) -> Self {
Self {
message: value.to_string(),
}
}
}
impl IntoResponse for MyError { .. }
type ApiResult<T, E = MyError> = Result<T, E>;
async fn index(mut req: viz::Request) -> ApiResult<&'static str> {
// since MyError implements From<viz::PayloadError> we can use '?'
let form = req.form::<RegisterForm>().await?;
Ok("Success")
} Which gives you a lot of flexibility over your errors |
Good idea. 👍 A lot of code needs to be redesigned(handler, router). I will try it. And PR is welcome! |
I think we can add a trait |
Axum's approach is to allow anything that implements |
https://viz.rs/en/0.4.x/concepts/error-handling
I think, the compromise solution is to add a |
Currently it does not work to return a custom error type that implements
IntoResponse
, it expects the error type to beviz::Error
Example
Version 0.4.17
The text was updated successfully, but these errors were encountered: