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 5A8C864B48 for ; Fri, 4 Mar 2022 09:48:39 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 498DE1DE7 for ; Fri, 4 Mar 2022 09:48:09 +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 A93101DDE for ; Fri, 4 Mar 2022 09:48:08 +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 6AAB941D86 for ; Fri, 4 Mar 2022 09:48:08 +0100 (CET) Date: Fri, 4 Mar 2022 09:48:06 +0100 From: Wolfgang Bumiller To: Dominik Csapak Cc: pbs-devel@lists.proxmox.com Message-ID: <20220304084806.75465zzr4cd2w5jh@wobu-vie.proxmox.com> References: <20220303145400.707298-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220303145400.707298-1-d.csapak@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.371 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 - Subject: [pbs-devel] applied: [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: Fri, 04 Mar 2022 08:48:39 -0000 applied, thanks On Thu, Mar 03, 2022 at 03:54:00PM +0100, Dominik Csapak wrote: > 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