From 747684be78ab182d5c78dfccc7fafbfff768959e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 1 Jun 2021 21:32:54 +0100 Subject: [PATCH] errors: Provide AnyhowFormat and use it in two places Signed-off-by: Ian Jackson --- src/error.rs | 2 +- src/sshkeys.rs | 4 ++-- src/utils.rs | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 73e353d4..65dc85ed 100644 --- a/src/error.rs +++ b/src/error.rs @@ -52,7 +52,7 @@ pub enum InternalError { StringFormatting(#[from] fmt::Error), #[error("JSON deserialisation error: {0:?}")] JSONEncode(serde_json::Error), - #[error("Server error: {0:?}")] + #[error("Server error: {}", AnyhowFormat(&.0))] Anyhow(#[from] anyhow::Error), #[error("Game contains only partial data for player, or account missing")] PartialPlayerData, diff --git a/src/sshkeys.rs b/src/sshkeys.rs index 21632935..83797f13 100644 --- a/src/sshkeys.rs +++ b/src/sshkeys.rs @@ -79,8 +79,8 @@ pub struct KeySpec { pub struct AuthKeysManipError { } impl From for AuthKeysManipError { fn from(ae: anyhow::Error) -> AuthKeysManipError { - error!("authorized_keys manipulation error: {}: {:?}", - &config().authorized_keys, ae); + error!("authorized_keys manipulation error: {}: {}", + &config().authorized_keys, AnyhowFormat(&ae)); AuthKeysManipError { } } } diff --git a/src/utils.rs b/src/utils.rs index 7a9f5e27..01f9bd73 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -643,6 +643,20 @@ impl IndexVec where I: index_vec::Idx { } } +#[derive(Debug)] +pub struct AnyhowFormat<'a>(pub &'a anyhow::Error); +impl Display for AnyhowFormat<'_> { + #[throws(fmt::Error)] + fn fmt(&self, f: &mut fmt::Formatter) { + let mut delim = ""; + self.0.for_each(&mut |s|{ + write!(f, "{}{}", delim, s)?; + delim = ": "; + Ok(()) + })?; + } +} + #[ext(pub)] impl anyhow::Error { fn for_each(&self, f: &mut dyn FnMut(&str) -> fmt::Result) -> fmt::Result { -- 2.30.2