From 15c3f477921317ec86eae51051d2f1c1ee65eae9 Mon Sep 17 00:00:00 2001 From: bytedream Date: Tue, 7 Jun 2022 10:38:58 +0200 Subject: [PATCH] Change error library to anyhow --- container/Cargo.lock | 127 ++-------------------------- container/Cargo.toml | 2 +- container/src/configure/cli/cli.rs | 3 +- container/src/shared/api/api.rs | 15 ++-- container/src/shared/api/request.rs | 3 +- 5 files changed, 18 insertions(+), 132 deletions(-) diff --git a/container/Cargo.lock b/container/Cargo.lock index 47d03db..aa9ccbb 100644 --- a/container/Cargo.lock +++ b/container/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "addr2line" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "ansi_term" version = "0.11.0" @@ -26,6 +11,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" + [[package]] name = "atty" version = "0.2.14" @@ -37,39 +28,12 @@ dependencies = [ "winapi", ] -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" - -[[package]] -name = "backtrace" -version = "0.3.63" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "321629d8ba6513061f26707241fa9bc89524ff1cd7a915a97ef0c62c666ce1b6" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "cc" -version = "1.0.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" - [[package]] name = "cfg-if" version = "1.0.0" @@ -95,7 +59,7 @@ dependencies = [ name = "docker4ssh" version = "0.1.0" dependencies = [ - "failure", + "anyhow", "log", "serde", "serde_json", @@ -103,34 +67,6 @@ dependencies = [ "structopt", ] -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "synstructure", -] - -[[package]] -name = "gimli" -version = "0.26.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" - [[package]] name = "heck" version = "0.3.3" @@ -176,31 +112,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "memchr" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - -[[package]] -name = "miniz_oxide" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg", -] - -[[package]] -name = "object" -version = "0.27.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9" -dependencies = [ - "memchr", -] - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -243,12 +154,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rustc-demangle" -version = "0.1.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" - [[package]] name = "ryu" version = "1.0.5" @@ -338,18 +243,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "unicode-xid", -] - [[package]] name = "textwrap" version = "0.11.0" @@ -377,12 +270,6 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - [[package]] name = "vec_map" version = "0.8.2" diff --git a/container/Cargo.toml b/container/Cargo.toml index bfae00e..1ea80ad 100644 --- a/container/Cargo.toml +++ b/container/Cargo.toml @@ -12,7 +12,7 @@ name = "configure" path = "src/configure/main.rs" [dependencies] -failure = "0.1" +anyhow = "1.0" log = "0.4" structopt = "0.3" serde = { version = "1.0", features = ["derive"]} diff --git a/container/src/configure/cli/cli.rs b/container/src/configure/cli/cli.rs index cb46ad2..e0aca9d 100644 --- a/container/src/configure/cli/cli.rs +++ b/container/src/configure/cli/cli.rs @@ -1,5 +1,6 @@ use std::process::Command; use std::time::SystemTime; +use anyhow::Result; use log::{info, warn}; use structopt::StructOpt; use structopt::clap::AppSettings; @@ -8,8 +9,6 @@ use crate::shared::api::api::API; use crate::shared::api::request; use crate::shared::api::request::{ConfigGetResponse, ConfigNetworkMode, ConfigRunLevel}; -type Result = std::result::Result; - trait Execute { fn execute(self, api: &mut API) -> Result<()>; } diff --git a/container/src/shared/api/api.rs b/container/src/shared/api/api.rs index 7abe2de..0770c05 100644 --- a/container/src/shared/api/api.rs +++ b/container/src/shared/api/api.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; use std::io::{Read, Write}; use std::net::TcpStream; +use anyhow::{anyhow, bail, Result}; use serde::Deserialize; -pub type Result = std::result::Result; - pub struct API { route: String, } @@ -19,7 +18,7 @@ impl API { pub fn new_connection(&mut self) -> Result { match TcpStream::connect(&self.route) { Ok(stream) => Ok(stream), - Err(e) => Err(failure::format_err!("Failed to connect to {}: {}", self.route, e.to_string())) + Err(e) => bail!("Failed to connect to {}: {}", self.route, e.to_string()) } } @@ -28,7 +27,7 @@ impl API { connection.write_all(request.as_string().as_bytes())?; let mut buf: String = String::new(); - connection.read_to_string(&mut buf).map_err(|e| failure::err_msg(e.to_string()))?; + connection.read_to_string(&mut buf)?; Ok(APIResult::new(request, buf)) } @@ -36,7 +35,7 @@ impl API { let result = self.request(request)?; if result.result_code >= 400 { let err: APIError = result.body()?; - Err(failure::err_msg(format!("Error {}: {}", result.result_code, err.message))) + bail!("Error {}: {}", result.result_code, err.message) } else { Ok(result) } @@ -80,15 +79,15 @@ impl APIResult { } pub fn body<'a, T: Deserialize<'a>>(&'a self) -> Result { - let result: T = serde_json::from_str(&self.result_body).map_err(|e| { + let result = serde_json::from_str(&self.result_body).map_err(|e| { // checks if the error has a body and if so, return it if self.has_body() { let error: APIError = serde_json::from_str(&self.result_body).unwrap_or_else(|_| { APIError{message: format!("could not deserialize response: {}", e.to_string())} }); - failure::format_err!("Failed to call '{}': {}", self.request_path, error.message) + anyhow!("Failed to call '{}': {}", self.request_path, error.message) } else { - failure::format_err!("Failed to call '{}': {}", self.request_path, e.to_string()) + anyhow!("Failed to call '{}': {}", self.request_path, e.to_string()) } })?; Ok(result) diff --git a/container/src/shared/api/request.rs b/container/src/shared/api/request.rs index 73327ee..fcca621 100644 --- a/container/src/shared/api/request.rs +++ b/container/src/shared/api/request.rs @@ -1,8 +1,9 @@ use std::fmt::{Display, Formatter}; +use anyhow::Result; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -use crate::shared::api::api::{API, Method, Request, Result}; +use crate::shared::api::api::{API, Method, Request}; use crate::shared::api::api::Method::POST; #[derive(Deserialize)]