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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id C0A2964722 for ; Thu, 3 Mar 2022 15:54:05 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B456E33CE2 for ; Thu, 3 Mar 2022 15:54:05 +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 id 9D4EF33CD7 for ; Thu, 3 Mar 2022 15:54:01 +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 6F5D446E84 for ; Thu, 3 Mar 2022 15:54:01 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 3 Mar 2022 15:54:00 +0100 Message-Id: <20220303145400.707298-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.154 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [schema.rs] Subject: [pbs-devel] [PATCH proxmox v3] proxmox-schema: add convenience macros for ParameterError 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: Thu, 03 Mar 2022 14:54:05 -0000 with two variants: (expr, expr) => assumes that the second is an 'Error' (expr, (tt)+) => passes the tt through anyhow::format_err also added tests Signed-off-by: Dominik Csapak --- changes from v2: * added variant with (expr,expr) to avoid format_err of an anyhow Error * fixed use of format_err (must use ::anyhow::format_err) * added tests proxmox-schema/src/schema.rs | 28 +++++++++++++++++++++++++++- proxmox-schema/tests/schema.rs | 13 +++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs index 0f90682..79b240f 100644 --- a/proxmox-schema/src/schema.rs +++ b/proxmox-schema/src/schema.rs @@ -21,6 +21,32 @@ 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, $err:expr) => { + $crate::ParameterError::from(($field, $err)) + }; + + ($field:expr, $($msg:tt)+) => { + $crate::ParameterError::from(($field, ::anyhow::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, $err:expr) => {{ + return Err($crate::param_format_err!($field, $err).into()); + }}; + + ($field:expr, $($msg:tt)+) => {{ + return Err($crate::param_format_err!($field, $($msg)+).into()); + }}; +} + impl std::error::Error for ParameterError {} impl ParameterError { @@ -538,7 +564,7 @@ impl ArraySchema { for (i, item) in list.iter().enumerate() { let result = self.items.verify_json(item); if let Err(err) = result { - return Err(ParameterError::from((format!("[{}]", i), err)).into()); + param_bail!(format!("[{}]", i), err); } } diff --git a/proxmox-schema/tests/schema.rs b/proxmox-schema/tests/schema.rs index ed09bb1..a0d7986 100644 --- a/proxmox-schema/tests/schema.rs +++ b/proxmox-schema/tests/schema.rs @@ -395,3 +395,16 @@ fn test_verify_complex_array() { assert!(res.is_err()); } } + +#[test] +fn test_parameter_error_macro() { + fn _bail_with_format() -> Result<(), anyhow::Error> { + let baz = "baz"; + param_bail!("foo", "bar: {}", baz); + } + + fn _bail_with_err() -> Result<(), anyhow::Error> { + let err = anyhow::format_err!("bar"); + param_bail!("foo", err); + } +} -- 2.30.2