I am writing a library to access an HTTP API asynchronous using hyper and Futures.
There is this one thing that I just can't wrap my head around: How to deal with errors before even starting the request / spawning the Future.
Here is an example of how I am writing the client implementation:
pub fn get(self) -> impl Future<Item = Component, Error = Error> {
let result = doing_some_checks();
if result.is_error() {
// The following obviously won't work:
return Err(Error::SomeErrorKind);
}
// safe to use
let something = result.unwrap();
let request = ... // creating hyper request
self.client
.request(request) // returns a Future
.and_then(|response| {
// doing stuff with the response ...
Ok(Component)
})
}
Is there any way to deal with errors before doing the HTTP request? It will have to do with returning some kind of an 'error' Future, but I could not find a working solution so far, a simple return futures::future::err(Error::ErrorKind)
won't work.