1

I'm implementing a generic error responder to handle errors in the rocket rust web framework.
Following the docs here: https://rocket.rs/v0.5-rc/guide/responses/#custom-responders

This responder works for me:

#[derive(Responder)]
#[response(status= 500, content_type = "json")]
struct ServerError {
    inner: String,
}

But I would like to make the status code dynamic. The docs state this:

Because ContentType and Status are themselves headers, you can also dynamically set the content-type and status by simply including fields of these types.

So I tried adding status to my struct like so:

use rocket::response::status;

#[derive(Responder)]
#[response(status= 500, content_type = "json")]
struct ServerError {
    inner: String,
    status: Status
}

#[get("/")]
async fn index() -> Result<String, ServerError> {
    Err(ServerError{
        inner: String::from("error"),
        status: Status::InternalServerError
    })
}

I get the following error:

47  |     status: Status
    |     ^^^^^^^^^^^^^^ the trait `From<Status>` is not implemented for `rocket::http::Header<'_>`

Is there a way to implement an error responder with a dynamic status without having to explicitly implement Responder and just relying on derive ?

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99

0 Answers0