chiark / gitweb /
errors: Provide AnyhowFormat and use it in two places
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 1 Jun 2021 20:32:54 +0000 (21:32 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 1 Jun 2021 20:32:54 +0000 (21:32 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/error.rs
src/sshkeys.rs
src/utils.rs

index 73e353d48d3f86e17912db5882fb44728dd9d779..65dc85ed7d61defdafab0b575fdc739d34b423c7 100644 (file)
@@ -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,
index 21632935e6bdf6befa63191e5716e8b093e07426..83797f13f8d126feb216d1c96a011ba8203b1cce 100644 (file)
@@ -79,8 +79,8 @@ pub struct KeySpec {
 pub struct AuthKeysManipError { }
 impl From<anyhow::Error> 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 { }
   }
 }
index 7a9f5e27c26588c64a0901a1c979482dbb87a314..01f9bd73ccdcbc976350e310bd773bbe5fa10e80 100644 (file)
@@ -643,6 +643,20 @@ impl<I,T> IndexVec<I,T> 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 {