[pbs-devel] [PATCH vma-to-pbs 09/10] refactor error handling with anyhow
Filip Schauer
f.schauer at proxmox.com
Wed Mar 20 15:15:15 CET 2024
Signed-off-by: Filip Schauer <f.schauer at 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
More information about the pbs-devel
mailing list