poem
poem copied to clipboard
Create a derive for custom errors
Description of the feature
You can create request params by deriving from Object
but you can not create custom errors yet, which are shown as errors in OpenApi and can be returned directly.
Maybe you can refer to this, ErrResponse
is a custom error.
https://github.com/poem-web/poem/blob/7ada5b69c94c0a98ddff64172703ed61c3386fdf/poem-openapi/tests/response.rs#L402
No, that does not work. I tried your solution, but no errors within the response sections (apart from the error codes).
What I did before:
#[derive(ApiResponse)]
enum UpdateUserResp {
/// everything is fine
#[oai(status = 200)]
Ok,
/// you are not logged in
#[oai(status = 401)]
#[allow(dead_code)]
Unauthorized,
/// you are not allowed to do that
#[oai(status = 403)]
#[allow(dead_code)]
Forbidden,
/// this user does not exist
#[oai(status = 404)]
NotFound,
/// this username is already taken
#[oai(status = 429)]
UserNameConflict(Json<UserNameConflictError>),
/// something went wrong
#[oai(status = 500)]
#[allow(dead_code)]
InternalServerError,
}
I added all errors to my response and returned them. It would be great, if you can
do something like this: #[oai(status = 401, error = true)]
and
#[derive(Debug, thiserror::Error, poem::ErrorObject)]
#[error("{message}")]
struct UserNameConflictError {
message: String,
}
impl ResponseError for UserNameConflictError {
fn status(&self) -> StatusCode {
StatusCode::CONFLICT
}
}
to create an error, that is actually shown as error like this:
Furthermore, it would be great, if the user can provide examples, like shown above. This should work for errors an non error responses as well.
I asked the creator of the API shown above: He does not create custom errors, but uses the example feature, which can be used for requests as well to archive that. So this is not a feature of Swagger directly, but a hack. The errors are not shown down below the other stuff nevertheless.