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 <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-05-12 12:06:18 +02:00 committed by Thomas Lamprecht
parent 440472cb32
commit 126ccbcfa6
1 changed files with 20 additions and 8 deletions

View File

@ -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
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)
}
/// 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)?;
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);