public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v2] proxmox-schema: add convenience macros for ParameterError
Date: Thu,  3 Mar 2022 15:23:57 +0100	[thread overview]
Message-ID: <20220303142357.447470-1-d.csapak@proxmox.com> (raw)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* format_param_err -> param_format_err
* drop use in this file to avoid format_err of an anyhow error

 proxmox-schema/src/schema.rs | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs
index 0f90682..ba7b30c 100644
--- a/proxmox-schema/src/schema.rs
+++ b/proxmox-schema/src/schema.rs
@@ -21,6 +21,24 @@ pub struct ParameterError {
     error_list: Vec<(String, Error)>,
 }
 
+/// Like anyhow's `format_err` but producing a `ParameterError`.
+#[macro_export]
+macro_rules! param_format_err {
+    ($field:expr, $($msg:tt)+) => {
+        $crate::ParameterError::from(($field, format_err!($($msg)+)))
+    };
+}
+
+/// Like anyhow's `bail` but enclosing a `ParameterError`, so
+/// a `downcast` can extract it later. This is useful for
+/// API calls that need to do parameter checking manually.
+#[macro_export]
+macro_rules! param_bail {
+    ($field:expr, $($msg:tt)+) => {{
+        return Err($crate::param_format_err!($field, $($msg)+).into());
+    }};
+}
+
 impl std::error::Error for ParameterError {}
 
 impl ParameterError {
-- 
2.30.2





                 reply	other threads:[~2022-03-03 14:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220303142357.447470-1-d.csapak@proxmox.com \
    --to=d.csapak@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