Skip to content
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

IntoResponses derivation could implement response traits #1266

Open
Shyrogan opened this issue Dec 28, 2024 · 2 comments
Open

IntoResponses derivation could implement response traits #1266

Shyrogan opened this issue Dec 28, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Shyrogan
Copy link

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:

#[derive(utoipa::IntoResponses, thiserror::Error, serde::Serialize, Debug)]
pub enum TranslationError {
    #[response(status = 500)]
    #[error("An error occured with postgres: {0]")]
    Postgres(String),
}

#[derive(utoipa::IntoResponses, utoipa::ToSchema, serde::Serialize, Debug)]
#[response(status = 200)]
pub struct Translation {
    message: String,
    translation: String,
}

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 return Result<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 case axum_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).

@Shyrogan
Copy link
Author

I think this is related: #649.

While the idea of auto responses from type definition is great, I think a good work around would be what's mentioned above.

@juhaku
Copy link
Owner

juhaku commented Jan 2, 2025

Would you be interested in such feature ? I'm willing to open a PR on my free time if so.

Sure, I am open for PRs. I haven't had a time to think through the implementation and currently there are some rough edges that would be nice to smoothen up.

Yes I'd say it is somewhat related, and what comes to the automatic responses, and I there as well I haven't had time to think the best possible way to support it. Since all the responses basically should implement probably ToResponse or IntoResponses types for it to work.

@juhaku juhaku added the enhancement New feature or request label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants