IntoResponses
derivation could implement response traits
#1266
Labels
enhancement
New feature or request
IntoResponses
derivation could implement response traits
#1266
Hello!
In the current state of error and response handling with various response types such as successful and error responses requires verbose code.
Typically, a
Result<Json<T>, (StatusCode, Json<E>)>
is used to return responses, which results in wack error handling logic in route handler functions and a repetition of status code.Consider the following code:
A more convenient approach would be to implement the
IntoResponse
trait for both error enums (like TranslationError) and response structures (like Translation). This would allow Axum to automatically convert these types into the appropriate HTTP responses, making the handler code cleaner and more maintainable (allowing the handler to returnResult<Translation, TranslationError>
).Another side effect of this would also to make sure the correct status code is sent when a result is sent.
Those implementations could be conditioned by the presence of the
framework_extras
flag (in this caseaxum_extras
)A prototype for a non-openapi compliant axum implementation can be found on a previous project I did: https://github.com/Shyrogan/axum_thiserror/blob/main/src/lib.rs.
Would you be interested in such feature ? I'm willing to open a PR on my free time if so.
Thank you for this awesome work! (Sorry if I missed an issue where this is mentioned).
The text was updated successfully, but these errors were encountered: