From 126ccbcfa6a37cbd4f44ba676e4580efb0e6b11c Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 12 May 2021 12:06:18 +0200 Subject: [PATCH] acme: improve errors when account loading fails 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 --- 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::load_path(account_path(account_name.as_ref())).await - } - - /// Load an existing ACME account by path. - async fn load_path(account_path: String) -> Result { - 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);