使用rust-websocket时如何处理错误,以便仅该连接失败而不是整个程序失败? [英] How do I handle an error when using rust-websocket so that only that connection fails and not the entire program?
问题描述
我正在尝试启动并运行Rust WebSocket服务器.我从异步websocker服务器的示例代码.
I am trying to get a Rust WebSocket server up and running. I started with the example code for an async websocker server.
每次我得到一个 Io
错误,例如当连接中断时,整个程序就结束而没有任何错误.我将示例第26行的代码修改为:
Every time I get an Io
error, like when the connection is interrupted, the entire program ends without any error. I modified the code on line 26 of the example to:
.map_err(|InvalidConnection {error, ..}| {
println!("Error:{:?}",error);
return error;
})
这会打印错误,但不会阻止程序仅停止单个连接并且不会崩溃.
This prints the error, but does not prevent the program from stopping only the single connection and not crashing itself.
我最常见的错误是:
Io(Error { repr: Custom(Custom { kind: Other, error: Stream(Error { repr: Custom(Custom { kind: ConnectionAborted, error: StringError("unexpected EOF observed") }) }) }) })
推荐答案
好,在rust IRC频道的帮助下,我找到了解决方案.我将错误替换为无"并过滤掉了.
Ok, with some help from rust IRC channel i found the solution. I've just replaced the error with None and filtered it out.
.map(Some).or_else(|_| -> Result<_, ()> { Ok(None) }).filter_map(|x| x)
也许此信息将帮助有类似问题的人.
Maybe this info will help someone with similar problem.
这篇关于使用rust-websocket时如何处理错误,以便仅该连接失败而不是整个程序失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!