* [pbs-devel] [PATCH proxmox] proxmox-schema: add convenience macros for ParameterError
@ 2022-03-03 11:53 Dominik Csapak
2022-03-03 12:52 ` Wolfgang Bumiller
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2022-03-03 11:53 UTC (permalink / raw)
To: pbs-devel
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox-schema/src/schema.rs | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs
index 58c6a82..c568580 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! format_param_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::format_param_err!($field, $($msg)+).into());
+ }};
+}
+
impl std::error::Error for ParameterError {}
impl ParameterError {
@@ -530,7 +548,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);
}
}
--
2.30.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [pbs-devel] [PATCH proxmox] proxmox-schema: add convenience macros for ParameterError
2022-03-03 11:53 [pbs-devel] [PATCH proxmox] proxmox-schema: add convenience macros for ParameterError Dominik Csapak
@ 2022-03-03 12:52 ` Wolfgang Bumiller
0 siblings, 0 replies; 2+ messages in thread
From: Wolfgang Bumiller @ 2022-03-03 12:52 UTC (permalink / raw)
To: Dominik Csapak; +Cc: pbs-devel
On Thu, Mar 03, 2022 at 12:53:37PM +0100, Dominik Csapak wrote:
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
> proxmox-schema/src/schema.rs | 20 +++++++++++++++++++-
> 1 file changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs
> index 58c6a82..c568580 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! format_param_err {
this makes:
- `bail` and `format_err`,
- `io_bail`, `io_format_err`, where `io_` is the common prefix
- `param_bail`, `format_param_err`, where `param_` is in two different places ;-)
please rename this to `param_format_err` for consistency
> + ($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::format_param_err!($field, $($msg)+).into());
> + }};
> +}
> +
> impl std::error::Error for ParameterError {}
>
> impl ParameterError {
> @@ -530,7 +548,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);
I don't really like the fact that the `err` which is already an
`anyhow::Error` is now wrapped in an extra `format_err!()` call.
I think this instance should probably not be changed to `param_bail!`.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-03-03 12:52 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 11:53 [pbs-devel] [PATCH proxmox] proxmox-schema: add convenience macros for ParameterError Dominik Csapak
2022-03-03 12:52 ` Wolfgang Bumiller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox