[pbs-devel] [PATCH proxmox v2] proxmox-schema: add convenience macros for ParameterError

Dominik Csapak d.csapak at proxmox.com
Thu Mar 3 15:23:57 CET 2022


Signed-off-by: Dominik Csapak <d.csapak at 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





More information about the pbs-devel mailing list