Catch connection timout instead of panic

I try to catch a connection timeout but struggling with that message from rust compiler

 expected enum `Result<tokio::net::TcpStream, std::io::Error>`

That's the code part which i want to change from panic() to a more recoverable error.

I have copied the timeout block from this SO answer.

Thanks for any help. :bowing_man:

Can you show your attempt that doesn't compile? I don't know how exactly you're trying to handle the timeout, but I would just do something like replacing the panic!() with return Err(/* something denoting a timeout */);.

Thanks for answer.

I have tried

                    /*
                     * TODO: Fix panic with better error handling
                     */
                    Err(e) => {
                        error!("in Elapsed: timeout while connecting to server : {:?}", e);
                        //panic!("{}", format!("timeout while connecting to server : {}", e))
                        return Err(e.into())
                    }

Which works for that block :slight_smile: but then the outbound isn't anymore a TcpStream.

The question is, is there a recoverable error with TcpStream when a timeout occur?

That's the compiler errors.

 RUST_LOG=info ENCODED_PW=My-Enc_PW TPT_CONFIG=container-files/etc/tpt/config.yaml cargo run
   Compiling tls-proxy-tunnel v3.0.0 (/datadisk/git-repos/tls-proxy-tunnel)
error[E0599]: no method named `peer_addr` found for unit type `()` in the current scope
  --> src/upstreams/proxy_to_upstream.rs:92:46
   |
92 |         debug!("Connected to {:?}", outbound.peer_addr().unwrap());
   |                                              ^^^^^^^^^ method not found in `()`

error[E0599]: no method named `set_nodelay` found for unit type `()` in the current scope
  --> src/upstreams/proxy_to_upstream.rs:94:18
   |
94 |         outbound.set_nodelay(true)?;
   |                  ^^^^^^^^^^^ method not found in `()`

error[E0599]: no method named `writable` found for unit type `()` in the current scope
   --> src/upstreams/proxy_to_upstream.rs:103:22
    |
103 |             outbound.writable().await?;
    |                      ^^^^^^^^ method not found in `()`

I think you need to append a ; to your return statement so that the compiler will properly treat it as diverging instead of an expression of type ().

I have keep the panic() for now in the code until I find a nicer solution.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.