Error handling with futures - returning multiple Error types

I'm using actix-web. I use failure for errors (if there is a better fit for future stuff, please tell me). Here is an async handler that uses reqwest::async to send another request.

fn import_data_set(params: web::Form<ImportDataSet>, log: web::Data<Arc<Logger>>) -> impl Future<Item = Json<Id>, Error = Error> {
    let log = Arc::clone(&log);
    info![*log, "Import data set"];
    let client = reqwest::r#async::Client::new();
    client.post("http://127.0.0.1:8002/api/v1/job")
        .json(&msg::JobInit::Scan {path: params.path.clone()})
        .send()
        .map_err(|e| e.into())
        .and_then(|mut res|{
            if res.status().is_success() {
                res.json::<Id>()
                   .map(move |id| {
                       info![*log, "Got id: {:?}", id];
                       Json(id)

                   })
                   .map_err(|e| e.into())
            } else {
                error![*log, "Scanner returned error {:?}", res];
                format_err!("Scanner returned error")
            }})
}

Errors:
error[E0308]: if and else have incompatible types
--> src/api_server.rs:53:17
|
43 | / if res.status().is_success() {
44 | | res.json::()
| |-
45 | || .map(move |id| {
46 | || info![*log, "Got id: {:?}", id];
47 | || Json(id)
48 | ||
49 | || })
50 | || .map_err(|e| e.into())
| ||
________________________- expected because of this
... |
53 | | format_err!("Scanner returned error")
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct futures::future::map_err::MapErr, found struct failure::Error
54 | | }})
| |_____________- if and else have incompatible types
|
= note: expected type futures::future::map_err::MapErr<futures::future::map::Map<impl futures::future::Future, [closure@src/api_server.rs:45:25: 49:21 log:_]>, [closure@src/api_server.rs:50:29: 50:41]>
found type failure::Error
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

(for some reason, preformatting the above errors doesn't work here. Temporarily hosted here: https://bpaste.net/show/Rk6b)

I understand why I get this error, but how can I fix it?
Also, is there something I can do better with respect to error handling in this code? I don't find it very nice with another indentation level with .map or and_then for every possible error.

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