简体   繁体   中英

How do I handle an error when using rust-websocket so that only that connection fails and not the entire program?

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") }) }) }) })

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM