Change error library to anyhow

This commit is contained in:
bytedream 2022-06-07 10:38:58 +02:00
parent 76ebcc65e0
commit 15c3f47792
5 changed files with 18 additions and 132 deletions

127
container/Cargo.lock generated
View File

@ -2,21 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.11.0" version = "0.11.0"
@ -26,6 +11,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "anyhow"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -37,39 +28,12 @@ dependencies = [
"winapi", "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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -95,7 +59,7 @@ dependencies = [
name = "docker4ssh" name = "docker4ssh"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"failure", "anyhow",
"log", "log",
"serde", "serde",
"serde_json", "serde_json",
@ -103,34 +67,6 @@ dependencies = [
"structopt", "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]] [[package]]
name = "heck" name = "heck"
version = "0.3.3" version = "0.3.3"
@ -176,31 +112,6 @@ dependencies = [
"cfg-if", "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]] [[package]]
name = "proc-macro-error" name = "proc-macro-error"
version = "1.0.4" version = "1.0.4"
@ -243,12 +154,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -338,18 +243,6 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -377,12 +270,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"

View File

@ -12,7 +12,7 @@ name = "configure"
path = "src/configure/main.rs" path = "src/configure/main.rs"
[dependencies] [dependencies]
failure = "0.1" anyhow = "1.0"
log = "0.4" log = "0.4"
structopt = "0.3" structopt = "0.3"
serde = { version = "1.0", features = ["derive"]} serde = { version = "1.0", features = ["derive"]}

View File

@ -1,5 +1,6 @@
use std::process::Command; use std::process::Command;
use std::time::SystemTime; use std::time::SystemTime;
use anyhow::Result;
use log::{info, warn}; use log::{info, warn};
use structopt::StructOpt; use structopt::StructOpt;
use structopt::clap::AppSettings; use structopt::clap::AppSettings;
@ -8,8 +9,6 @@ use crate::shared::api::api::API;
use crate::shared::api::request; use crate::shared::api::request;
use crate::shared::api::request::{ConfigGetResponse, ConfigNetworkMode, ConfigRunLevel}; use crate::shared::api::request::{ConfigGetResponse, ConfigNetworkMode, ConfigRunLevel};
type Result<T> = std::result::Result<T, failure::Error>;
trait Execute { trait Execute {
fn execute(self, api: &mut API) -> Result<()>; fn execute(self, api: &mut API) -> Result<()>;
} }

View File

@ -1,10 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::TcpStream; use std::net::TcpStream;
use anyhow::{anyhow, bail, Result};
use serde::Deserialize; use serde::Deserialize;
pub type Result<T> = std::result::Result<T, failure::Error>;
pub struct API { pub struct API {
route: String, route: String,
} }
@ -19,7 +18,7 @@ impl API {
pub fn new_connection(&mut self) -> Result<TcpStream> { pub fn new_connection(&mut self) -> Result<TcpStream> {
match TcpStream::connect(&self.route) { match TcpStream::connect(&self.route) {
Ok(stream) => Ok(stream), 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())?; connection.write_all(request.as_string().as_bytes())?;
let mut buf: String = String::new(); 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)) Ok(APIResult::new(request, buf))
} }
@ -36,7 +35,7 @@ impl API {
let result = self.request(request)?; let result = self.request(request)?;
if result.result_code >= 400 { if result.result_code >= 400 {
let err: APIError = result.body()?; let err: APIError = result.body()?;
Err(failure::err_msg(format!("Error {}: {}", result.result_code, err.message))) bail!("Error {}: {}", result.result_code, err.message)
} else { } else {
Ok(result) Ok(result)
} }
@ -80,15 +79,15 @@ impl APIResult {
} }
pub fn body<'a, T: Deserialize<'a>>(&'a self) -> Result<T> { pub fn body<'a, T: Deserialize<'a>>(&'a self) -> Result<T> {
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 // checks if the error has a body and if so, return it
if self.has_body() { if self.has_body() {
let error: APIError = serde_json::from_str(&self.result_body).unwrap_or_else(|_| { let error: APIError = serde_json::from_str(&self.result_body).unwrap_or_else(|_| {
APIError{message: format!("could not deserialize response: {}", e.to_string())} 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 { } 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) Ok(result)

View File

@ -1,8 +1,9 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr}; 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; use crate::shared::api::api::Method::POST;
#[derive(Deserialize)] #[derive(Deserialize)]