paperclip icon indicating copy to clipboard operation
paperclip copied to clipboard

Actix plugin: Response codes other than 200

Open wafflespeanut opened this issue 5 years ago • 1 comments

With the current actix-web flow, we can (atmost) get two types of status codes through Result<T, E> and for everything else, we only have one status code. So, we need to think about how we can represent error models used by the server.

wafflespeanut avatar Jul 28 '19 13:07 wafflespeanut

I just came across this limitation and am having a go at solving it for my use case.

There are more complicated use cases such as services which could return several different types of successful responses and in those cases it would be better to have real descriptions, but I don't think I'll need those any time soon.

The solution i'm attempting is a series of structs implementing Responder and Apiv2Schema which repeat the implementations on Json + one for StatusCode::NO_CONTENT. I like that there is no ambiguity here, you either get the status code set on both the responder and on paperclip or on neither.

So a handler becomes:

#[api_v2_operation]
pub async fn handle() -> Result<CreatedJson<JustMessage>, Error> {
    Ok(CreatedJson(JustMessage {
        message: "Successful.",
    }))
}

Implementation in gist : https://gist.github.com/platy/e0bc44e0ce91c8268dcf0d9689e1f974

platy avatar Oct 01 '20 11:10 platy