Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rocket's Responder trait is not implemented for Result

I've recently started learning Rust, and I'm currently trying to create a small API; I've made my own struct for an API Response and enum for an Error:

// API Response Structure
pub struct ApiResponse {
    pub body: JsonValue,
    pub status: Status,
}

// Enum for my "custom errors"
#[derive(Debug, Snafu, Clone, Serialize)]
pub enum Errors {
    #[snafu(display("Unable to retrieve the specified entry."))]
    NotFound,

    #[snafu(display("Internal Server Error, unable to process the data."))]
    ISE,
}

Then I've implemented the Responder<'_> trait for my API Response struct:

impl<'r> Responder<'r> for ApiResponse {
    fn respond_to(self, req: &Request) -> Result<Response<'r>, Status> {
        Response::build_from(self.body.respond_to(req).unwrap())
            .status(self.status)
            .header(ContentType::JSON)
            .ok()
    }
}

However, when I try to use the mentioned, it would seem as I cannot have my function return a Result<JsonValue, Errors>. I'm not quite sure about this issue, nor am I that experienced with Rust, so any documentation/pointers will be greatly appreciated.

Here's the function which returns the Response type.

#[put("/users/<user_id>", format = "json", data = "<new_user>")]
pub fn update_user(
    conn: DbConnection,
    user_id: i32,
    new_user: Json<NewUser>,
) -> Result<JsonValue, ApiResponse> {

    match users::table.find(user_id).load::<User>(&*conn) {
        Ok(result) => {
            if result.len() < 1 {
                let response = ApiResponse {
                    body: json!({
                        "message": Errors::NotFound
                    }),
                    status: Status::NotFound
                };

                return Err(response)
            }
        },

        Err(e) => {
            let response = ApiResponse {
                body: json!({
                    "message": Errors::ISE
                }),
                status: Status::InternalServerError
            };

            return Err(response);
        },
    }

    match diesel::update(users::table.filter(id.eq(user_id)))
        .set((
            user_name.eq(new_user.user_name.to_string()),
            age.eq(new_user.age),
            gender.eq(new_user.gender.to_string()),
        ))
        .get_result::<User>(&*conn)
    {
        Ok(result) => Ok(json!(result)),
        Err(_) => {
            let res = ApiResponse {
                body: json!({
                    "message": Errors::ISE
                }),
                status: Status::InternalServerError
            };

            return Err(res);
        },
    }
}

Side-note: Please keep in mind that I'm still a beginner with Rust, and my error handling / code in general is not the best.


Edit: I forgot to include the error stack:

error[E0277]: the trait bound `std::result::Result<rocket_contrib::json::JsonValue, api_cont::ApiResponse>: rocket::response::Responder<'_>` is not satisfied
   --> src/routes.rs:72:6
    |
72  | ) -> Result<JsonValue, ApiResponse> {
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `rocket::response::Responder<'_>` is not implemented for `std::result::Result<rocket_contrib::json::JsonValue, api_cont::ApiResponse>`
    | 
   ::: /home/kenneth/.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.4.4/src/handler.rs:202:20
    |
202 |     pub fn from<T: Responder<'r>>(req: &Request, responder: T) -> Outcome<'r> {
    |                    ------------- required by this bound in `rocket::handler::<impl rocket::Outcome<rocket::Response<'r>, rocket::http::Status, rocket::Data>>::from`
    |
    = help: the following implementations were found:
              <std::result::Result<R, E> as rocket::response::Responder<'r>>
              <std::result::Result<R, E> as rocket::response::Responder<'r>>
like image 941
kernel_dev Avatar asked Sep 12 '25 09:09

kernel_dev


1 Answers

I've run into this error as well. The following is mentioned in the API Docs:

A type implementing Responder should implement the Debug trait when possible. This is because the Responder implementation for Result requires its Err type to implement Debug. Therefore, a type implementing Debug can more easily be composed.

That means you must implement Debug, as you use ApiResponse as an Err type.

#[derive(Debug)]
pub struct ApiResponse {
    pub body: JsonValue,
    pub status: Status,
}
like image 198
peterulb Avatar answered Sep 14 '25 23:09

peterulb