error-handling - 如何处理 rust 上的盒装和链式错误?

标签 error-handling rust hyper reqwest

我有很多代理 uri(http 和 socks5),我正在使用 reqwest 通过这些代理发送一些 http 请求,如果他不工作我想放弃代理。

for proxy in proxies {
    let proxy = match Proxy::all(proxy_url) {
        Ok(proxy) => proxy,
        Err(e) => {
            eprintln!("creating proxy failed! {:?}", e);
            continue;
        }
    };

    let client = match Client::builder()
    .proxy(proxy)
    .build()

    let client = match client {
        Ok(client) => client,
        Err(e) => {
            eprintln!("building client failed! {:?}", e);
            continue;
        }
    }

    loop {
        match client.get(&config.endpoint).send().await {
            Ok(_) => {}
            Err(e) => {
                if e.is_proxy_error() { // this method doesn't exist
                    eprintln!("Dropping proxy...");
                    break;
                } else {
                    eprintln!("client error! {:?}", e);
                }
            }
        }
    }
}

我有很多种 Reqwest::Error

reqwest::Error { kind: Request, url: "http://example.com/", source: hyper::Error(Connect, "socks connect error: Invalid response version") }
reqwest::Error { kind: Request, url: "http://example.com/", source: hyper::Error(Connect, "socks connect error: Proxy server unreachable") }
reqwest::Error { kind: Request, url: "http://example.com/", source: hyper::Error(Connect, "socks connect error: Connection refused") }
reqwest::Error { kind: Request, url: "http://example.com/", source: hyper::Error(Connect, "socks connect error: Connection reset by peer (os error 104)") }

在大多数情况下,错误消息是明确的,但我如何以不同的方式处理它们中的每一个? reqwest::Errorinner私有(private)领域,所以我无法访问他。 如果我得到 reqwest::Error 的来源, 我只有一个 Box<syn Error>我不能把它当成 hyper::Error

最佳答案

您可以将 Error::source() 的结果向下转换为具体的错误类型,例如

use std::error::Error;

...

    Err(reqwest_error) => {
        let hyper_error: Option<&hyper::Error> = reqwest_error
            .source()
            .unwrap()
            .downcast_ref();
        ...
    }

此代码使用 unwrap()source() 返回的 Option 中提取错误。如果你不能保证错误总是有一个来源,你应该使用一些有条件的方式来解包来源,例如

let hyper_error: Option<&hyper::Error> = reqwest_error
    .source()
    .and_then(|e| e.downcast_ref());

关于error-handling - 如何处理 rust 上的盒装和链式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61099090/

相关文章:

testing - 有没有办法检测是否从 Rust 中的测试调用了代码?

rust - 如何使用 Hyper 通过代理访问 HTTPS 站点?

rust - 为什么在 hyper 中匹配请求路径后没有任何反应?

python - 语法错误-使用边界框坐标裁剪图像

python - Python –没有错误但也没有返回数据

function - Rust 函数如何修改数组索引的值?

rust - 我如何绑定(bind) Higher Rank Trait Bound 生命周期?

rust - 如何将 hyper::server::Server 存储为结构中的字段?

javascript - 有没有办法检查链接、img、脚本是否加载失败?

ruby-on-rails - Ruby给了我类似其他人的四个失败