Help with reqwest.json()? and custom errors

Hello everybody, first post and new rust project at work! :partying_face: So, naive alert here!! :crazy_face:

I’m trying to implement the custom error wrapping described here: looks like a very good and scalable way to deal with error handling (not so easy for a rust newcomer).

I have a function that calls an external service with reqwest and try to deserialize the result in a custom type. Here it is:

fn fetch_person(id: i32) -> SwapiResult<SwapiPerson> {
    let url = format!("{}", id);

SwapiResult is defined like this:

type SwapiResult<T> = Result<T, SwapiError>;

and SwapiError is an enum modeled like the one in the guide above.

The compiler is telling me that I should implement the deserialize trait on the error. It’s not clear to me why it’s trying to deserialize the body of the response into an error, instead of just returning the Err(reqwest::Error) converted in a SwapiError.

this is the error I’m getting:

error[E0277]: the trait bound `controller::person::SwapiError: models::_IMPL_SERIALIZE_FOR_Person::_serde::Deserialize<'_>` is not satisfied
  --> src/controller/
66 |     reqwest::get(&url)?.json()?
   |                         ^^^^ the trait `models::_IMPL_SERIALIZE_FOR_Person::_serde::Deserialize<'_>` is not implemented for `controller::person::SwapiError`
   = note: required because of the requirements on the impl of `for<'de> models::_IMPL_SERIALIZE_FOR_Person::_serde::Deserialize<'de>` for `std::result::Result<swapi_models::person::Person, controller::person::SwapiError>`
   = note: required because of the requirements on the impl of `models::_IMPL_SERIALIZE_FOR_Person::_serde::de::DeserializeOwned` for `std::result::Result<swapi_models::person::Person, controller::person::SwapiError>`

What’s the problem here? Many thanks in advance!

You’re fetching SwapiPerson, but your function needs to return SwapiResult, so return Ok(reqwest::get(….

You get this weird error, because .json() works “backwards” from the return type, and it sees it’s asked to give you SwapiResult, but it doesn’t know how.

1 Like

but…isn’t the ? responsible for wrapping everything in a Result?

No, ? is the opposite. It removes things out of a Result<T, …> to bare T.

1 Like

thanks, it’s working…I’m feeling stupid :sweat_smile:

No problem.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.