Convert serde_json::Error to hyper::Error
Sorry if this is an inconvenient place to ask questions like this.
I'm learning all the nicies of Rust 2018 Epoch, and I stumbled over a error:
pub fn func(&self) -> Result<MyItem, failure::Error> {
let uri: Uri = ...;
let future = self.http
.get(uri)
.and_then(|result| result.into_body().concat2())
.and_then(|body| {
let item: MyItem = ::serde_json::from_slice(&body)?;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the trait `std::convert::From<serde_json::Error>`
is not implemented for `hyper::Error`
Ok(item)
});
...
}
Can you help me resolve the issue (I suspect quite a newbie one)?
For now I resolved it with
::serde_json::from_slice(&body).expect("valid json")
But what if I don't want panic?
EDIT: I missed that this is client side, not server side. Ignore me.
Your last and_then is called on a impl Future<Error = hyper::Error> so its closure is expected to return an impl IntoFuture<Error = hyper::Error>. Since you return an Ok(item), it is inferred to return a Result<_, hyper::Error>.
to be able to use the ? syntax on a Result<_, serde_json::Error> inside that closure, you would need a way to convert from serde_json::Error to hyper::Error, and that's pretty much what the compiler is telling you.
Since hyper::Error and serde_json::Error come from external crate, you cannot write an impl From<serde_json::Error> for hyper::Error
I see that you are using failure::Error though, it provides an impl of From for both hyper::Error and serde_json::Error. So what you could do is map the future's error to a failure::Error using map_err before doing your deserializing:
fn work() -> Result<MyItem, failure::Error> {
let client = hyper::Client::new();
let uri = "...".parse()?; // here ? works too cause there is an impl From<hyper::Error> for failure::Error
let future = client.get(uri)
.and_then(|result| result.into_body().concat2())
.map_err(failure::Error::from) // Now we have a Future<Error = failure::Error>
.and_then(|body| { // Infered to return a Result<MyItem, failure::Error>
let item: MyItem = ::serde_json::from_slice(&body)?; // now works cause there is an impl From<serde_json::Error> for failure::Error
// ...
Ok(item)
});
future.wait()
}
@khuey
This isn't really something you can do. It's better to convert your serde error to a useful Response (at a minimum, an HTTP 500 error code) rather than attempting to return an error to hyper.
Good point, although this will hide the exact error (which I may be not interested in, but still).
EDIT: I missed that this is client side, not server side. Ignore me.
Yeah, for server side what you said is what I would do.
@Globidev This is what I was looking for, thank you for the generous explanation! I've seen .map_errs in various examples, but they were usually written after all and_thens. Also, I didn't know there is future.wait().
This resolves my question.
It's worth remembering that Future::wait is convenient in some places, but it definitely SHOULD NOT be used if this is part of server code. Best case, you pause all concurrent requests on your server for some time, worse case, you deadlock the server if the future you're waiting for needs the current thread to be polling a different future in order to fullfill it.
@seanmonstar Thanks for your note, I agree. For now, while I'm trying to get a grasp on Rust futures and how tokio works in general, I'm more interested in client side and simple console apps. I have a strong background with C# with its Tasks and async/await, and now I want to learn how the same things look like and work in Rust.
In the above func, I did want to convert an async operation to a sync result in a blocking way, so I think Future::wait fits perfectly here. Toy servers, non-blocking event loops and similar concepts are my next step. :)