-
Notifications
You must be signed in to change notification settings - Fork 324
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
Implement IntoResponse for Result #408
Conversation
c6c12b9
to
cd9d9e3
Compare
I don't think the errors here are related to the PR. |
cd9d9e3
to
423412b
Compare
@Licenser thanks to #438 we now enable handling Also worth sharing here is that we may want to remove |
Amusingly enough I think this might be related to #444 I'm currently battling it and would say it doesn't work at all since nothing compiles and error messages are cryptic 😂 but I feel like I'm missing something obvious. |
OKay with that lead I found the issue in #444 but no that doesn't work since it doesn't allow adding a status code to the error which this PR would. |
unless the error in |
✨ https://docs.rs/http-types/1.2.0/http_types/struct.Error.html#method.status |
Oh, I didn't see that, however, it still misses control over things like content type (returning JSON vs human errors ) but I suppose it'd be more sensible to extend I also can not implement From, that on an error that already implements I'm not married to IntoResponse but the ergonomics it provides are rather good and with #438 a lot of them get lost which, at least for me, makes using surf a lot more painful then it was before. (edit) |
To ilustrate the code now looks like this: app.at("/version")
.get(|r| async { Ok(api::version::get(r).await.into_response()) });
app.at("/binding")
.get(|r| async { Ok(api::binding::list_artefact(r).await.into_response()) })
.post(|r| async { Ok(api::binding::publish_artefact(r).await.into_response()) });
app.at("/binding/{aid}")
.get(|r| async { Ok(api::binding::get_artefact(r).await.into_response()) })
.delete(|r| async { Ok(api::binding::unpublish_artefact(r).await.into_response()) });
app.at("/binding/{aid}/{sid}")
.get(|r| async { Ok(api::binding::get_servant(r).await.into_response()) })
.post(|r| async { Ok(api::binding::link_servant(r).await.into_response()) })
.delete(|r| async { Ok(api::binding::unlink_servant(r).await.into_response()) });
app.at("/pipeline")
.get(|r| async { Ok(api::pipeline::list_artefact(r).await.into_response()) })
.post(|r| async { Ok(api::pipeline::publish_artefact(r).await.into_response()) });
app.at("/pipeline/{aid}")
.get(|r| async { Ok(api::pipeline::get_artefact(r).await.into_response()) })
.delete(|r| async { Ok(api::pipeline::unpublish_artefact(r).await.into_response()) });
app.at("/onramp")
.get(|r| async { Ok(api::onramp::list_artefact(r).await.into_response()) })
.post(|r| async { Ok(api::onramp::publish_artefact(r).await.into_response()) });
app.at("/onramp/{aid}")
.get(|r| async { Ok(api::onramp::get_artefact(r).await.into_response()) })
.delete(|r| async { Ok(api::onramp::unpublish_artefact(r).await.into_response()) });
app.at("/offramp")
.get(|r| async { Ok(api::offramp::list_artefact(r).await.into_response()) })
.post(|r| async { Ok(api::offramp::publish_artefact(r).await.into_response()) });
app.at("/offramp/{aid}")
.get(|r| async { Ok(api::offramp::get_artefact(r).await.into_response()) })
.delete(|r| async { Ok(api::offramp::unpublish_artefact(r).await.into_response()) }); to set up a somewhat more complex endpoint that does still allow setting headers on errors. prior to #438 it was: app.at("/version").get(api::version::get);
app.at("/binding")
.get(api::binding::list_artefact)
.post(api::binding::publish_artefact);
app.at("/binding/{aid}")
.get(api::binding::get_artefact)
.delete(api::binding::unpublish_artefact);
app.at("/binding/{aid}/{sid}")
.get(api::binding::get_servant)
.post(api::binding::link_servant)
.delete(api::binding::unlink_servant);
app.at("/pipeline")
.get(api::pipeline::list_artefact)
.post(api::pipeline::publish_artefact);
app.at("/pipeline/{aid}")
.get(api::pipeline::get_artefact)
.delete(api::pipeline::unpublish_artefact);
app.at("/onramp")
.get(api::onramp::list_artefact)
.post(api::onramp::publish_artefact);
app.at("/onramp/{aid}")
.get(api::onramp::get_artefact)
.delete(api::onramp::unpublish_artefact);
app.at("/offramp")
.get(api::offramp::list_artefact)
.post(api::offramp::publish_artefact);
app.at("/offramp/{aid}")
.get(api::offramp::get_artefact)
.delete(api::offramp::unpublish_artefact); I hope that puts a bit of perspective on the lost ergonomics of the newer version of tide. |
We already have an automatic conversion of I see two ways around this:
fn convert(f: impl Fn() -> std::io::Result<()>) -> http_types::Result<()> {
let res = f()?;
Ok(res)
} Regarding the final part: if I'm understanding it correctly, what you're looking for is a way to convert the text from the error to JSON. If you're going to use |
Implementing IntoResponse for Result allows for Endpoints to use results and by that the
?
operator which makes it more ergonomic to use with error handling.