public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH vma-to-pbs 6/9] refactor error handling
Date: Wed,  3 Apr 2024 11:49:10 +0200	[thread overview]
Message-ID: <20240403094913.107177-7-f.schauer@proxmox.com> (raw)
In-Reply-To: <20240403094913.107177-1-f.schauer@proxmox.com>

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/vma.rs     | 14 +++++++-------
 src/vma2pbs.rs | 33 ++++++++++++++++++---------------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/src/vma.rs b/src/vma.rs
index 447e8db..f7944cc 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, bail, Result};
+use anyhow::{bail, Context, Result};
 use bincode::Options;
 use serde::{Deserialize, Serialize};
 use serde_big_array::BigArray;
@@ -135,11 +135,11 @@ impl<T: Read> VmaReader<T> {
         let mut vma_header: VmaHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_header.magic != VMA_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         if vma_header.version != 1 {
-            return Err(anyhow!("Invalid VMA version {}", vma_header.version));
+            bail!("Invalid VMA version {}", vma_header.version);
         }
 
         buffer.resize(vma_header.header_size as usize, 0);
@@ -150,7 +150,7 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA header checksum"));
+            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<T: Read> VmaReader<T> {
         let vma_extent_header: VmaExtentHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_extent_header.magic != VMA_EXTENT_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         // Fill the MD5 sum field with zeros to compute the MD5 sum
@@ -241,7 +241,7 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_extent_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA extent header checksum"));
+            bail!("Wrong VMA extent header checksum");
         }
 
         Ok(vma_extent_header)
@@ -303,7 +303,7 @@ impl<T: Read> VmaReader<T> {
                         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");
                         }
                     }
                     _ => {
diff --git a/src/vma2pbs.rs b/src/vma2pbs.rs
index 5b9e20f..9483f6e 100644
--- a/src/vma2pbs.rs
+++ b/src/vma2pbs.rs
@@ -39,6 +39,16 @@ struct BlockDeviceInfo {
     pub device_size: u64,
 }
 
+fn handle_pbs_error(pbs_err: *mut c_char, function_name: &str) -> Result<()> {
+    if pbs_err.is_null() {
+        bail!("{function_name} failed without error message");
+    }
+
+    let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
+    let pbs_err_str = pbs_err_cstr.to_string_lossy();
+    bail!("{function_name} failed: {pbs_err_str}");
+}
+
 fn create_pbs_backup_task(args: BackupVmaToPbsArgs) -> Result<*mut ProxmoxBackupHandle> {
     println!("PBS repository: {}", args.pbs_repository);
     println!("PBS fingerprint: {}", args.fingerprint);
@@ -88,8 +98,7 @@ fn create_pbs_backup_task(args: BackupVmaToPbsArgs) -> Result<*mut ProxmoxBackup
     );
 
     if pbs.is_null() {
-        let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-        bail!("proxmox_backup_new_ns failed: {pbs_err_cstr:?}");
+        handle_pbs_error(pbs_err, "proxmox_backup_new_ns")?;
     }
 
     Ok(pbs)
@@ -117,8 +126,7 @@ where
             &mut pbs_err,
         ) < 0
         {
-            let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-            bail!("proxmox_backup_add_config failed: {pbs_err_cstr:?}");
+            handle_pbs_error(pbs_err, "proxmox_backup_add_config")?;
         }
     }
 
@@ -158,8 +166,7 @@ where
         );
 
         if pbs_device_id < 0 {
-            let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-            bail!("proxmox_backup_register_image failed: {pbs_err_cstr:?}");
+            handle_pbs_error(pbs_err, "proxmox_backup_register_image")?;
         }
 
         let block_device_info = BlockDeviceInfo {
@@ -254,11 +261,10 @@ where
             );
 
             if write_data_result < 0 {
-                let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-                bail!("proxmox_backup_write_data failed: {pbs_err_cstr:?}");
+                handle_pbs_error(pbs_err, "proxmox_backup_write_data")?;
             }
 
-            Ok(())
+            Ok::<(), anyhow::Error>(())
         };
 
         let insert_image_chunk = |image_chunks: &mut HashMap<u64, ImageChunk>,
@@ -325,8 +331,7 @@ where
         let pbs_device_id = block_device_info.pbs_device_id;
 
         if proxmox_backup_close_image(pbs, pbs_device_id, &mut pbs_err) < 0 {
-            let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-            bail!("proxmox_backup_close_image failed: {pbs_err_cstr:?}");
+            handle_pbs_error(pbs_err, "proxmox_backup_close_image")?;
         }
     }
 
@@ -353,8 +358,7 @@ pub fn backup_vma_to_pbs(args: BackupVmaToPbsArgs) -> Result<()> {
     let connect_result = proxmox_backup_connect(pbs, &mut pbs_err);
 
     if connect_result < 0 {
-        let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-        bail!("proxmox_backup_connect failed: {pbs_err_cstr:?}");
+        handle_pbs_error(pbs_err, "proxmox_backup_connect")?;
     }
 
     println!("Connected to Proxmox Backup Server");
@@ -365,8 +369,7 @@ pub fn backup_vma_to_pbs(args: BackupVmaToPbsArgs) -> Result<()> {
     upload_block_devices(vma_reader, pbs)?;
 
     if proxmox_backup_finish(pbs, &mut pbs_err) < 0 {
-        let pbs_err_cstr = unsafe { CStr::from_ptr(pbs_err) };
-        bail!("proxmox_backup_finish failed: {pbs_err_cstr:?}");
+        handle_pbs_error(pbs_err, "proxmox_backup_finish")?;
     }
 
     let elapsed_ms = SystemTime::now()
-- 
2.39.2





  parent reply	other threads:[~2024-04-03  9:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-03  9:49 [pbs-devel] [PATCH vma-to-pbs 0/9] Implement vma-to-pbs tool Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 1/9] Add the ability to provide credentials via files Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 2/9] bump proxmox-backup-qemu Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 3/9] remove unnecessary "extern crate" declarations Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 4/9] add support for streaming the VMA file via stdin Filip Schauer
2024-04-03 14:52   ` Max Carrara
2024-04-09 12:16     ` Filip Schauer
2024-04-09 13:06       ` Max Carrara
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 5/9] add a fallback for the --fingerprint argument Filip Schauer
2024-04-03 14:52   ` Max Carrara
2024-04-03  9:49 ` Filip Schauer [this message]
2024-04-03 14:52   ` [pbs-devel] [PATCH vma-to-pbs 6/9] refactor error handling Max Carrara
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 7/9] makefile: remove reference to unused submodule Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 8/9] switch argument handling from clap to pico-args Filip Schauer
2024-04-03 14:52   ` Max Carrara
2024-04-09 12:16     ` Filip Schauer
2024-04-03  9:49 ` [pbs-devel] [PATCH vma-to-pbs 9/9] reformat command line arguments to kebab-case Filip Schauer
2024-04-03  9:57 ` [pbs-devel] [PATCH vma-to-pbs 0/9] Implement vma-to-pbs tool Filip Schauer
2024-04-03 14:51 ` Max Carrara
2024-04-09 12:17   ` 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=20240403094913.107177-7-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal