【发布时间】:2021-02-11 21:05:59
【问题描述】:
所以我在 Rust 中有一个多线程程序,它向我的网站发送 Get 请求,我想知道如何检测 ConnectionReset。 我要做的是,在请求之后,检查是否有 ConnectionReset,如果有,请等待一分钟,这样线程就不会恐慌
我现在使用的代码
let mut req = reqwest::get(&url).unwrap();
在执行完之后,我想检查是否有 ConnectionReset,然后 println(“连接错误”),而不是让线程恐慌。
我要检测的错误
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value:
Error { kind: Io(Custom { kind: Other, error: Os { code: 10054, kind: ConnectionReset,
message: "An existing connection was forcibly closed by the remote host." } }),
url: Some("https://tayhay.vip") }', src\main.rs:22:43
我还阅读了一些关于 std::panic::catch_unwind 的内容,但我不确定这是否是正确的方法。
【问题讨论】:
-
通过调用
.unwrap()你明确告诉 Rust 恐慌。