From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 4DDF8BB757 for ; Mon, 25 Mar 2024 13:35:50 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 316BA3E01 for ; Mon, 25 Mar 2024 13:35:20 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 25 Mar 2024 13:35:19 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id F045A41F64 for ; Mon, 25 Mar 2024 13:35:18 +0100 (CET) Content-Type: text/plain; charset=UTF-8 Date: Mon, 25 Mar 2024 13:35:17 +0100 Message-Id: From: "Max Carrara" To: "Proxmox Backup Server development discussion" Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: aerc 0.17.0-72-g6a84f1331f1c References: <20240320141516.213930-1-f.schauer@proxmox.com> <20240320141516.213930-10-f.schauer@proxmox.com> In-Reply-To: <20240320141516.213930-10-f.schauer@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.021 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [main.rs, vma.rs] Subject: Re: [pbs-devel] [PATCH vma-to-pbs 09/10] refactor error handling with anyhow X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Mar 2024 12:35:50 -0000 On Wed Mar 20, 2024 at 3:15 PM CET, Filip Schauer wrote: > Signed-off-by: Filip Schauer > --- > 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; > =20 > @@ -89,7 +89,7 @@ fn main() -> Result<()> { > =20 > let pbs_password =3D match password_file { > Some(password_file) =3D> { > - std::fs::read_to_string(password_file).expect("Could not rea= d password file") > + std::fs::read_to_string(password_file).context("Could not re= ad password file")? > } > None =3D> String::from_utf8(tty::read_password("Password: ")?)?, > }; > @@ -100,7 +100,7 @@ fn main() -> Result<()> { > =20 > Some(match key_password_file { > Some(key_password_file) =3D> std::fs::read_to_string(key= _password_file) > - .expect("Could not read key password file"), > + .context("Could not read key password file")?, > None =3D> String::from_utf8(tty::read_password("Key Pass= word: ")?)?, > }) > } > 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; > =20 > -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 =3D bincode_options.deserialize(&b= uffer)?; > =20 > if vma_header.magic !=3D VMA_HEADER_MAGIC { > - return Err(anyhow!("Invalid magic number")); > + anyhow::bail!("Invalid magic number"); You could've just imported `bail` above. ;) > } > =20 > if vma_header.version !=3D 1 { > - return Err(anyhow!("Invalid VMA version {}", vma_header.vers= ion)); > + anyhow::bail!("Invalid VMA version {}", vma_header.version); > } > =20 > buffer.resize(vma_header.header_size as usize, 0); > @@ -150,7 +150,7 @@ impl VmaReader { > let computed_md5sum: [u8; 16] =3D md5::compute(&buffer).into(); > =20 > if vma_header.md5sum !=3D computed_md5sum { > - return Err(anyhow!("Wrong VMA header checksum")); > + anyhow::bail!("Wrong VMA header checksum"); > } > =20 > let blob_buffer =3D &buffer[VMA_HEADER_SIZE_NO_BLOB_BUFFER..vma_= header.header_size as usize]; > @@ -233,7 +233,7 @@ impl VmaReader { > let vma_extent_header: VmaExtentHeader =3D bincode_options.deser= ialize(&buffer)?; > =20 > if vma_extent_header.magic !=3D VMA_EXTENT_HEADER_MAGIC { > - return Err(anyhow!("Invalid magic number")); > + anyhow::bail!("Invalid magic number"); > } > =20 > // Fill the MD5 sum field with zeros to compute the MD5 sum > @@ -241,7 +241,7 @@ impl VmaReader { > let computed_md5sum: [u8; 16] =3D md5::compute(&buffer).into(); > =20 > if vma_extent_header.md5sum !=3D computed_md5sum { > - return Err(anyhow!("Wrong VMA extent header checksum")); > + anyhow::bail!("Wrong VMA extent header checksum"); > } > =20 > Ok(vma_extent_header) > @@ -303,7 +303,7 @@ impl VmaReader { > if ioerr.kind() =3D=3D std::io::ErrorKind::Unexp= ectedEof { > break; // Break out of the loop since the en= d 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"); > } > } > _ =3D> {