From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH vma-to-pbs 09/10] refactor error handling with anyhow
Date: Wed, 20 Mar 2024 15:15:15 +0100 [thread overview]
Message-ID: <20240320141516.213930-10-f.schauer@proxmox.com> (raw)
In-Reply-To: <20240320141516.213930-1-f.schauer@proxmox.com>
Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
src/main.rs | 6 +++---
src/vma.rs | 14 +++++++-------
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 149c1a6..e44257f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use anyhow::Result;
+use anyhow::{Context, Result};
use clap::{command, Arg, ArgAction};
use proxmox_sys::linux::tty;
@@ -89,7 +89,7 @@ fn main() -> Result<()> {
let pbs_password = match password_file {
Some(password_file) => {
- std::fs::read_to_string(password_file).expect("Could not read password file")
+ std::fs::read_to_string(password_file).context("Could not read password file")?
}
None => String::from_utf8(tty::read_password("Password: ")?)?,
};
@@ -100,7 +100,7 @@ fn main() -> Result<()> {
Some(match key_password_file {
Some(key_password_file) => std::fs::read_to_string(key_password_file)
- .expect("Could not read key password file"),
+ .context("Could not read key password file")?,
None => String::from_utf8(tty::read_password("Key Password: ")?)?,
})
}
diff --git a/src/vma.rs b/src/vma.rs
index d369b36..fca6586 100644
--- a/src/vma.rs
+++ b/src/vma.rs
@@ -3,7 +3,7 @@ use std::io::Read;
use std::mem::size_of;
use std::str;
-use anyhow::{anyhow, Result};
+use anyhow::{Context, Result};
use bincode::Options;
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
@@ -135,11 +135,11 @@ impl VmaReader {
let mut vma_header: VmaHeader = bincode_options.deserialize(&buffer)?;
if vma_header.magic != VMA_HEADER_MAGIC {
- return Err(anyhow!("Invalid magic number"));
+ anyhow::bail!("Invalid magic number");
}
if vma_header.version != 1 {
- return Err(anyhow!("Invalid VMA version {}", vma_header.version));
+ anyhow::bail!("Invalid VMA version {}", vma_header.version);
}
buffer.resize(vma_header.header_size as usize, 0);
@@ -150,7 +150,7 @@ impl VmaReader {
let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
if vma_header.md5sum != computed_md5sum {
- return Err(anyhow!("Wrong VMA header checksum"));
+ anyhow::bail!("Wrong VMA header checksum");
}
let blob_buffer = &buffer[VMA_HEADER_SIZE_NO_BLOB_BUFFER..vma_header.header_size as usize];
@@ -233,7 +233,7 @@ impl VmaReader {
let vma_extent_header: VmaExtentHeader = bincode_options.deserialize(&buffer)?;
if vma_extent_header.magic != VMA_EXTENT_HEADER_MAGIC {
- return Err(anyhow!("Invalid magic number"));
+ anyhow::bail!("Invalid magic number");
}
// Fill the MD5 sum field with zeros to compute the MD5 sum
@@ -241,7 +241,7 @@ impl VmaReader {
let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
if vma_extent_header.md5sum != computed_md5sum {
- return Err(anyhow!("Wrong VMA extent header checksum"));
+ anyhow::bail!("Wrong VMA extent header checksum");
}
Ok(vma_extent_header)
@@ -303,7 +303,7 @@ impl VmaReader {
if ioerr.kind() == std::io::ErrorKind::UnexpectedEof {
break; // Break out of the loop since the end of the file was reached.
} else {
- return Err(anyhow!("Failed to read VMA file: {}", ioerr));
+ return Err(anyhow::format_err!(e)).context("Failed to read VMA file");
}
}
_ => {
--
2.39.2
next prev parent reply other threads:[~2024-03-20 14:16 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-20 14:15 [pbs-devel] [PATCH v5 vma-to-pbs 00/10] Implement vma-to-pbs tool Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 01/10] Initial commit Filip Schauer
2024-03-25 9:42 ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 02/10] cargo fmt Filip Schauer
2024-03-25 9:43 ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 03/10] bump proxmox-backup-qemu Filip Schauer
2024-03-25 9:43 ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 04/10] Add the ability to provide credentials via files Filip Schauer
2024-03-25 12:33 ` Max Carrara
2024-03-25 15:26 ` Thomas Lamprecht
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 05/10] bump proxmox-backup-qemu Filip Schauer
2024-03-25 12:33 ` Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 06/10] remove unnecessary "extern crate" declarations Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 07/10] add support for streaming the VMA file via stdin Filip Schauer
2024-03-25 12:34 ` Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 08/10] add a fallback for the --fingerprint argument Filip Schauer
2024-03-25 12:35 ` Max Carrara
2024-03-20 14:15 ` Filip Schauer [this message]
2024-03-25 12:35 ` [pbs-devel] [PATCH vma-to-pbs 09/10] refactor error handling with anyhow Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 10/10] Makefile: remove reference to unused submodule Filip Schauer
2024-03-25 12:38 ` [pbs-devel] [PATCH v5 vma-to-pbs 00/10] Implement vma-to-pbs tool Max Carrara
2024-03-25 12:52 ` Wolfgang Bumiller
2024-04-03 9:56 ` Filip Schauer
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=20240320141516.213930-10-f.schauer@proxmox.com \
--to=f.schauer@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