axum icon indicating copy to clipboard operation
axum copied to clipboard

debug_handler: Check IntoResponseParts / IntoResponse for return tuple element types

Open jplatte opened this issue 2 years ago • 10 comments

In Jon Gjengset's Decrusting the axum crate video, he tried using #[debug_handler] to get a more useful error message for (StatusCode, Json<_>, AppendHeaders<_>) not implementing IntoResponse as part of a handler, but that didn't work. I think we can make it work.

jplatte avatar Aug 14 '23 11:08 jplatte

Is anyone working on this? I'd like to implement this if not.

brian030128 avatar Sep 01 '23 12:09 brian030128

I don’t think so. You’re welcome to submit a PR!

davidpdrsn avatar Sep 01 '23 12:09 davidpdrsn

Thanks for the opportunity! I improved the error message from

error[E0277]: the trait bound `(StatusCode, Json<&str>, AppendHeaders<[(HeaderName, &str); 1]>): IntoResponse` is not satisfied
 --> tests/debug_handler/fail/wrong_return_tuple_type.rs:5:23
  |
5 |   async fn handler() -> (
  |  _______________________^
6 | |     axum::http::StatusCode,
7 | |     axum::Json<&'static str>,
8 | |     axum::response::AppendHeaders<[( axum::http::HeaderName,&'static str); 1]>,
9 | | ) {
  | |_^ the trait `IntoResponse` is not implemented for `(StatusCode, Json<&str>, AppendHeaders<[(HeaderName, &str); 1]>)`

to

error[E0277]: the trait bound `Json<&'static str>: IntoResponseParts` is not satisfied
 --> tests/debug_handler/fail/wrong_return_tuple_type.rs:7:5
  |
7 |     axum::Json<&'static str>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoResponseParts` is not implemented for `Json<&'static str>`

It checks the first element => StatusCode || Parts || IntoResponseParts last element => IntoResponse other elements => IntoResponseParts and also the max numbers of IntoResponseParts(16)

I'll start to add tests if you guys think this is a good approach.

brian030128 avatar Sep 01 '23 18:09 brian030128

That sounds good, but I think for the extractors we also have special checks for Json and other body extractors of known names where we then print a custom error message if they're not last. Those kinds of messages would be helpful here as well, I think.

jplatte avatar Sep 01 '23 19:09 jplatte

Yes I agree that would be good but not essential and a PR without is also cool. We can always add that later.

davidpdrsn avatar Sep 01 '23 19:09 davidpdrsn

I was having the same thought when writing it, but I'm not sure what types should always be placed at last.So I added a //todo where the logic should be written.

brian030128 avatar Sep 01 '23 20:09 brian030128

Added special checks for known types that implements IntoResponse but not IntoResponseParts, but it only checks for Json and String now , the list can be extended. Also opened a pr , let me know if there's change needed!

brian030128 avatar Sep 02 '23 14:09 brian030128

Sorry this is my first time doing a pull request. Should I request a specific reviewer, or just leave it like that?

brian030128 avatar Sep 05 '23 15:09 brian030128

Nope you don’t need to do anything. I’ll take a look this week :)

davidpdrsn avatar Sep 05 '23 15:09 davidpdrsn

Got it, thanks!

brian030128 avatar Sep 05 '23 15:09 brian030128