Skip to content

Commit

Permalink
Change from failure to thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jun 15, 2022
1 parent d09e5f2 commit 4769d25
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 286 deletions.
102 changes: 1 addition & 101 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ ssl = ["openssl", "native-tls", "hyper-tls"]

[dependencies]
chrono = "0.4"
failure = "0.1"
futures = "0.3"
http = "0.2"
hyper = { version = "0.14", features = ["client", "http1", "stream", "tcp"] }
Expand All @@ -45,9 +44,9 @@ native-tls = { version = "0.2", optional = true }
nix = "0.24"
base64 = "0.13"
dirs = "4"
thiserror = "1"

[dev-dependencies]
failure = "0.1"
env_logger = "0.9"
rand = "0.8"

Expand Down
11 changes: 2 additions & 9 deletions examples/findports.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extern crate dockworker;
extern crate failure;

use dockworker::errors::*;
use dockworker::{container::ContainerFilters, Docker};
use failure::Fail;

fn find_all_exported_ports() -> Result<()> {
let docker = Docker::connect_with_defaults()?;
Expand All @@ -22,11 +20,6 @@ fn find_all_exported_ports() -> Result<()> {
Ok(())
}

fn main() {
if let Err(err) = find_all_exported_ports() {
eprint!("Error: ");
for e in <dyn Fail>::iter_causes(&err) {
eprintln!("{}", e);
}
}
fn main() -> Result<()> {
find_all_exported_ports()
}
34 changes: 19 additions & 15 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn default_cert_path() -> Result<PathBuf> {
if let Ok(ref path) = from_env {
Ok(PathBuf::from(path))
} else {
let home = dirs::home_dir().ok_or_else(|| ErrorKind::NoCertPath)?;
let home = dirs::home_dir().ok_or_else(|| Error::NoCertPath)?;
Ok(home.join(".docker"))
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Docker {
Docker::connect_with_http(&host)
}
} else {
Err(ErrorKind::UnsupportedScheme { host: host.clone() }.into())
Err(Error::UnsupportedScheme { host: host.clone() }.into())
}
}

Expand All @@ -209,33 +209,36 @@ impl Docker {

#[cfg(not(unix))]
pub fn connect_with_unix(addr: &str) -> Result<Docker> {
Err(ErrorKind::UnsupportedScheme {
Err(Error::UnsupportedScheme {
host: addr.to_owned(),
}
.into())
}

#[cfg(feature = "openssl")]
pub fn connect_with_ssl(addr: &str, key: &Path, cert: &Path, ca: &Path) -> Result<Docker> {
let client = HyperClient::connect_with_ssl(addr, key, cert, ca).context(
ErrorKind::CouldNotConnect {
let client = HyperClient::connect_with_ssl(addr, key, cert, ca).map_err(|err| {
Error::CouldNotConnect {
addr: addr.to_owned(),
},
)?;
source: err.into(),
}
})?;
Ok(Docker::new(client, Protocol::Tcp))
}

#[cfg(not(feature = "openssl"))]
pub fn connect_with_ssl(_addr: &str, _key: &Path, _cert: &Path, _ca: &Path) -> Result<Docker> {
Err(ErrorKind::SslDisabled.into())
Err(Error::SslDisabled.into())
}

/// Connect using unsecured HTTP. This is strongly discouraged
/// everywhere but on Windows when npipe support is not available.
pub fn connect_with_http(addr: &str) -> Result<Docker> {
let client = HyperClient::connect_with_http(addr).context(ErrorKind::CouldNotConnect {
addr: addr.to_owned(),
})?;
let client =
HyperClient::connect_with_http(addr).map_err(|err| Error::CouldNotConnect {
addr: addr.to_owned(),
source: err.into(),
})?;
Ok(Docker::new(client, Protocol::Tcp))
}

Expand Down Expand Up @@ -745,8 +748,9 @@ impl Docker {
.get("X-Docker-Container-Path-Stat")
.map(|h| h.to_str().unwrap_or(""))
.unwrap_or("");
let bytes = base64::decode(stat_base64).context(ErrorKind::ParseError {
let bytes = base64::decode(stat_base64).map_err(|err| Error::ParseError {
input: String::from(stat_base64),
source: err,
})?;
let path_stat: XDockerContainerPathStat = serde_json::from_slice(&bytes)?;
Ok(path_stat)
Expand Down Expand Up @@ -1029,13 +1033,13 @@ impl Docker {
// looking for file name like XXXXXXXXXXXXXX.json
if path.extension() == Some(OsStr::new("json")) && path != Path::new("manifest.json") {
let stem = path.file_stem().unwrap(); // contains .json
let id = stem.to_str().ok_or(ErrorKind::Unknown {
let id = stem.to_str().ok_or(Error::Unknown {
message: format!("convert to String: {:?}", stem),
})?;
return Ok(ImageId::new(id.to_string()));
}
}
Err(ErrorKind::Unknown {
Err(Error::Unknown {
message: "no expected file: XXXXXX.json".to_owned(),
}
.into())
Expand Down Expand Up @@ -1713,7 +1717,7 @@ mod tests {
assert!(match docker.get_file(&container.id, test_file) {
Ok(_) => false,
Err(err) => {
if let ErrorKind::Docker = err.kind() {
if let Error::Docker(_) = err {
true // not found
} else {
false
Expand Down
Loading

0 comments on commit 4769d25

Please sign in to comment.