Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub enum ServerError {
#[error("error reading or writing data")]
ProtocolError,

#[error("failed to connect to server")]
FailedToConnect,
#[error("failed to connect to server: {source}")]
FailedToConnect { source: std::io::Error },

#[error("invalid JSON response: \"{0}\"")]
InvalidJson(String),
Expand Down Expand Up @@ -144,9 +144,11 @@ impl ConnectionConfig {

/// Connects to the server and consumes the builder.
pub async fn connect(self) -> Result<StatusConnection, ServerError> {
let stream = TcpStream::connect(format!("{}:{}", self.address, self.port))
.await
.map_err(|_| ServerError::FailedToConnect)?;
let Ok(stream) = tokio::time::timeout(self.timeout, TcpStream::connect(format!("{}:{}", self.address, self.port))).await else {
return Err(ServerError::FailedToConnect { source: std::io::ErrorKind::TimedOut.into()});
};

let stream = stream.map_err(|e| ServerError::FailedToConnect { source: e })?;

Ok(StatusConnection {
stream,
Expand Down