How to match on an error from error_chain?


#1

I have got the following definition of custom errors:

error_chain! {
    errors {
        NegativeResponse(status: ::hyper::StatusCode, body: String) {
            display("unexpected negative response {}, {}", status, body)
        }
    }
}

I construct an instance of this like the following (notice into call, it does not compile without it):

let result: errors::Result<()> = Err(errors::ErrorKind::NegativeResponse(
    hyper::StatusCode::Unauthorized, "").into())

Now I can match like this:

match result {
    Ok(_) => info!("OK"),
    Err(e) => error!("Error {:?}", e),
}

But how can I match for exactly NegativeResponse error? Things I tried do not work (compiler errors of different kinds):

match result {
    Ok(_) => info!("OK"),
    Err(errors::ErrorKind::NegativeResponse(hyper::StatusCode::Unauthorized, _).into()) => error!("Error"),
    Err(errors::ErrorKind::NegativeResponse(hyper::StatusCode::Unauthorized, _)) => error!("Error"),
    Err(errors::ErrorKind::NegativeResponse(code, body).into()) => error!("Error"),
    Err(errors::ErrorKind::NegativeResponse(code, body)) => error!("Error"),
    errors::ErrorKind::NegativeResponse(code, body) => error!("Error"),
    _ => error!("Error {:?}", e),
}


#2

Solved. This combination works:

match result {
    Err(errors::Error(errors::ErrorKind::NegativeResponse(hyper::StatusCode::Unauthorized, _), _)) => { ... }
}