Skip to content

Commit

Permalink
Return a custom error from connect_to_docker
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 28, 2024
1 parent d7370d8 commit 9717201
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
26 changes: 19 additions & 7 deletions cmd/soroban-cli/src/commands/network/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion {
minor_version: 40,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("⛔ ️Failed to start container: {0}")]
BollardErr(#[from] bollard::errors::Error),

#[error("URI scheme is not supported: {uri}")]
UnsupportedURISchemeError { uri: String },
}

#[derive(ValueEnum, Debug, Clone, PartialEq)]
pub enum Network {
Local,
Expand All @@ -41,10 +50,11 @@ impl fmt::Display for Network {
}
}

pub async fn connect_to_docker(
docker_host: &Option<String>,
) -> Result<Docker, bollard::errors::Error> {
// defaults to "unix:///var/run/docker.sock" if no docker_host is provided
pub async fn connect_to_docker(docker_host: &Option<String>) -> Result<Docker, Error> {
// if no docker_host is provided, use the default docker host:
// "unix:///var/run/docker.sock" on unix machines
// "npipe:////./pipe/docker_engine" on windows machines

let host = docker_host
.clone()
.unwrap_or(DEFAULT_DOCKER_HOST.to_string());
Expand All @@ -66,8 +76,10 @@ pub async fn connect_to_docker(
Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
}
_ => {
// default to connecting with socket defaults
Docker::connect_with_socket_defaults()
return Err(Error::UnsupportedURISchemeError {
uri: host.to_string(),
}
.into());
}
}?;

Expand All @@ -85,7 +97,7 @@ pub async fn connect_to_docker(
)?;
match check_docker_connection(&connection).await {
Ok(_) => return Ok(connection),
Err(err) => return Err(err),
Err(err) => return Err(err)?,
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/soroban-cli/src/commands/network/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ use bollard::{
};
use futures_util::TryStreamExt;

use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_HOST_HELP};
use crate::commands::network::shared::{
connect_to_docker, Error as ConnectionError, Network, DOCKER_HOST_HELP,
};

const DEFAULT_PORT_MAPPING: &str = "8000:8000";
const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart";

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("⛔ ️Failed to start container: {0}")]
StartContainerError(#[from] bollard::errors::Error),
#[error("⛔ ️Failed to connect to docker: {0}")]
ConnectionError(#[from] ConnectionError),

#[error("⛔ ️Failed to create container: {0}")]
BollardErr(#[from] bollard::errors::Error),
}

#[derive(Debug, clap::Parser, Clone)]
Expand Down
6 changes: 4 additions & 2 deletions cmd/soroban-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_HOST_HELP};
use crate::commands::network::shared::{
connect_to_docker, Error as ConnectionError, Network, DOCKER_HOST_HELP,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to stop container: {0}")]
StopContainerError(#[from] bollard::errors::Error),
StopContainerError(#[from] ConnectionError),
}

#[derive(Debug, clap::Parser, Clone)]
Expand Down

0 comments on commit 9717201

Please sign in to comment.