* [pbs-devel] [PATCH proxmox v3] proxmox-schema: add convenience macros for ParameterError
@ 2022-03-03 14:54 Dominik Csapak
2022-03-04 8:48 ` [pbs-devel] applied: " Wolfgang Bumiller
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2022-03-03 14:54 UTC (permalink / raw)
To: pbs-devel
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 <d.csapak@proxmox.com>
---
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* [pbs-devel] applied: [PATCH proxmox v3] proxmox-schema: add convenience macros for ParameterError
2022-03-03 14:54 [pbs-devel] [PATCH proxmox v3] proxmox-schema: add convenience macros for ParameterError Dominik Csapak
@ 2022-03-04 8:48 ` Wolfgang Bumiller
0 siblings, 0 replies; 2+ messages in thread
From: Wolfgang Bumiller @ 2022-03-04 8:48 UTC (permalink / raw)
To: Dominik Csapak; +Cc: pbs-devel
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 <d.csapak@proxmox.com>
> ---
> 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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-03-04 8:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 14:54 [pbs-devel] [PATCH proxmox v3] proxmox-schema: add convenience macros for ParameterError Dominik Csapak
2022-03-04 8:48 ` [pbs-devel] applied: " Wolfgang Bumiller
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal