public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup] acme: improve errors when account loading fails
Date: Wed, 12 May 2021 12:06:18 +0200	[thread overview]
Message-ID: <20210512100618.7526-1-w.bumiller@proxmox.com> (raw)

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@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





             reply	other threads:[~2021-05-12 10:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 10:06 Wolfgang Bumiller [this message]
2021-05-12 10:23 ` [pbs-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210512100618.7526-1-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal