How do I handle an error when using rust-websocket

2019-07-29 00:30发布

I am trying to get a Rust WebSocket server up and running. I started with the example code for an async websocker server.

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.

The most common error I get is:

Io(Error { repr: Custom(Custom { kind: Other, error: Stream(Error { repr: Custom(Custom { kind: ConnectionAborted, error: StringError("unexpected EOF observed") }) }) }) })

1条回答
看我几分像从前
2楼-- · 2019-07-29 00:48

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.

查看更多
登录 后发表回答