[pbs-devel] [PATCH backup] acme: improve errors when account loading fails

Wolfgang Bumiller w.bumiller at proxmox.com
Wed May 12 12:06:18 CEST 2021


if the account does not exist, error with its name
if file loading fails, the error includes the full path
if the content fails to parse, show file & parse error
and in each case mention that it's about loading the acme account file

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/acme/client.rs | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/src/acme/client.rs b/src/acme/client.rs
index 91ef8b2a..28f277e9 100644
--- a/src/acme/client.rs
+++ b/src/acme/client.rs
@@ -4,7 +4,7 @@ use std::fs::OpenOptions;
 use std::io;
 use std::os::unix::fs::OpenOptionsExt;
 
-use anyhow::format_err;
+use anyhow::{bail, format_err};
 use bytes::Bytes;
 use hyper::{Body, Request};
 use nix::sys::stat::Mode;
@@ -78,13 +78,25 @@ impl AcmeClient {
 
     /// Load an existing ACME account by name.
     pub async fn load(account_name: &AcmeAccountName) -> Result<Self, anyhow::Error> {
-        Self::load_path(account_path(account_name.as_ref())).await
-    }
-
-    /// Load an existing ACME account by path.
-    async fn load_path(account_path: String) -> Result<Self, anyhow::Error> {
-        let data = tokio::fs::read(&account_path).await?;
-        let data: AccountData = serde_json::from_slice(&data)?;
+        let account_path = account_path(account_name.as_ref());
+        let data = match tokio::fs::read(&account_path).await {
+            Ok(data) => data,
+            Err(err) if err.kind() == io::ErrorKind::NotFound => {
+                bail!("acme account '{}' does not exist", account_name)
+            }
+            Err(err) => bail!(
+                "failed to load acme account from '{}' - {}",
+                account_path,
+                err
+            ),
+        };
+        let data: AccountData = serde_json::from_slice(&data).map_err(|err| {
+            format_err!(
+                "failed to parse acme account from '{}' - {}",
+                account_path,
+                err
+            )
+        })?;
 
         let account = Account::from_parts(data.location, data.key, data.account);
 
-- 
2.20.1






More information about the pbs-devel mailing list