[pbs-devel] [PATCH proxmox-backup 1/2] api/config: use param_bail for parameter errors

Dominik Csapak d.csapak at proxmox.com
Fri Mar 4 14:47:50 CET 2022


when using the 'extjs' formatter, it marks them in a way, so that
the gui can mark the form fields with the error

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/api2/config/access/openid.rs        |  4 ++--
 src/api2/config/acme.rs                 |  6 +++---
 src/api2/config/changer.rs              | 10 +++++-----
 src/api2/config/datastore.rs            |  4 ++--
 src/api2/config/drive.rs                | 12 ++++++------
 src/api2/config/media_pool.rs           |  4 ++--
 src/api2/config/remote.rs               |  6 +++---
 src/api2/config/sync.rs                 |  4 ++--
 src/api2/config/tape_backup_job.rs      |  4 ++--
 src/api2/config/tape_encryption_keys.rs | 10 +++++-----
 src/api2/config/traffic_control.rs      |  4 ++--
 src/api2/config/verify.rs               |  4 ++--
 12 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/src/api2/config/access/openid.rs b/src/api2/config/access/openid.rs
index 25dfffbf..1e5b5519 100644
--- a/src/api2/config/access/openid.rs
+++ b/src/api2/config/access/openid.rs
@@ -6,7 +6,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     OpenIdRealmConfig, OpenIdRealmConfigUpdater,
@@ -68,7 +68,7 @@ pub fn create_openid_realm(config: OpenIdRealmConfig) -> Result<(), Error> {
         config.realm == "pam" ||
         domains.sections.get(&config.realm).is_some()
     {
-        bail!("realm '{}' already exists.", config.realm);
+        param_bail!("realm", "realm '{}' already exists.", config.realm);
     }
 
     domains.set_data(&config.realm, "openid", &config)?;
diff --git a/src/api2/config/acme.rs b/src/api2/config/acme.rs
index 2fe1664f..5e3b4b3f 100644
--- a/src/api2/config/acme.rs
+++ b/src/api2/config/acme.rs
@@ -13,7 +13,7 @@ use hex::FromHex;
 use proxmox_router::{
     http_bail, list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap,
 };
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 use proxmox_sys::{task_log, task_warn};
 
 use proxmox_acme_rs::account::AccountData as AcmeAccountData;
@@ -582,7 +582,7 @@ pub fn get_plugin(id: String, mut rpcenv: &mut dyn RpcEnvironment) -> Result<Plu
 pub fn add_plugin(r#type: String, core: DnsPluginCore, data: String) -> Result<(), Error> {
     // Currently we only support DNS plugins and the standalone plugin is "fixed":
     if r#type != "dns" {
-        bail!("invalid ACME plugin type: {:?}", r#type);
+        param_bail!("type", "invalid ACME plugin type: {:?}", r#type);
     }
 
     let data = String::from_utf8(base64::decode(&data)?)
@@ -594,7 +594,7 @@ pub fn add_plugin(r#type: String, core: DnsPluginCore, data: String) -> Result<(
 
     let (mut plugins, _digest) = plugin::config()?;
     if plugins.contains_key(&id) {
-        bail!("ACME plugin ID {:?} already exists", id);
+        param_bail!("id", "ACME plugin ID {:?} already exists", id);
     }
 
     let plugin = serde_json::to_value(DnsPlugin { core, data })?;
diff --git a/src/api2/config/changer.rs b/src/api2/config/changer.rs
index fe00c1eb..f3513011 100644
--- a/src/api2/config/changer.rs
+++ b/src/api2/config/changer.rs
@@ -4,7 +4,7 @@ use serde_json::Value;
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, ScsiTapeChanger, ScsiTapeChangerUpdater, LtoTapeDrive,
@@ -43,11 +43,11 @@ pub fn create_changer(config: ScsiTapeChanger) -> Result<(), Error> {
 
     for changer in existing {
         if changer.name == config.name {
-            bail!("Entry '{}' already exists", config.name);
+            param_bail!("name", "Entry '{}' already exists", config.name);
         }
 
         if changer.path == config.path {
-            bail!("Path '{}' already in use by '{}'", config.path, changer.name);
+            param_bail!("path", "Path '{}' already in use by '{}'", config.path, changer.name);
         }
     }
 
@@ -252,7 +252,7 @@ pub fn delete_changer(name: String, _param: Value) -> Result<(), Error> {
     match config.sections.get(&name) {
         Some((section_type, _)) => {
             if section_type != "changer" {
-                bail!("Entry '{}' exists, but is not a changer device", name);
+                param_bail!("name", "Entry '{}' exists, but is not a changer device", name);
             }
             config.sections.remove(&name);
         },
@@ -263,7 +263,7 @@ pub fn delete_changer(name: String, _param: Value) -> Result<(), Error> {
     for drive in drive_list {
         if let Some(changer) = drive.changer {
             if changer == name {
-                bail!("Delete changer '{}' failed - used by drive '{}'", name, drive.name);
+                param_bail!("name", "Delete changer '{}' failed - used by drive '{}'", name, drive.name);
             }
         }
     }
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index f8f98afe..0ec84423 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -6,7 +6,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, RpcEnvironmentType, Permission};
-use proxmox_schema::{api, ApiType};
+use proxmox_schema::{api, param_bail, ApiType};
 use proxmox_section_config::SectionConfigData;
 use proxmox_sys::WorkerTaskContext;
 
@@ -113,7 +113,7 @@ pub fn create_datastore(
     let (section_config, _digest) = pbs_config::datastore::config()?;
 
     if section_config.sections.get(&config.name).is_some() {
-        bail!("datastore '{}' already exists.", config.name);
+        param_bail!("name", "datastore '{}' already exists.", config.name);
     }
 
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
diff --git a/src/api2/config/drive.rs b/src/api2/config/drive.rs
index 1c92e4c3..68dc8d06 100644
--- a/src/api2/config/drive.rs
+++ b/src/api2/config/drive.rs
@@ -1,10 +1,10 @@
-use anyhow::{bail, Error};
+use anyhow::{bail, format_err, Error};
 use ::serde::{Deserialize, Serialize};
 use serde_json::Value;
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, LtoTapeDrive, LtoTapeDriveUpdater, ScsiTapeChanger,
@@ -43,10 +43,10 @@ pub fn create_drive(config: LtoTapeDrive) -> Result<(), Error> {
 
     for drive in existing {
         if drive.name == config.name {
-            bail!("Entry '{}' already exists", config.name);
+            param_bail!("name", "Entry '{}' already exists", config.name);
         }
         if drive.path == config.path {
-            bail!("Path '{}' already used in drive '{}'", config.path, drive.name);
+            param_bail!("path", "Path '{}' already used in drive '{}'", config.path, drive.name);
         }
     }
 
@@ -218,7 +218,7 @@ pub fn update_drive(
             data.changer_drivenum = None;
         } else {
             if data.changer.is_none() {
-                bail!("Option 'changer-drivenum' requires option 'changer'.");
+                param_bail!("changer", format_err!("Option 'changer-drivenum' requires option 'changer'."));
             }
             data.changer_drivenum = Some(changer_drivenum);
         }
@@ -254,7 +254,7 @@ pub fn delete_drive(name: String, _param: Value) -> Result<(), Error> {
     match config.sections.get(&name) {
         Some((section_type, _)) => {
             if section_type != "lto" {
-                bail!("Entry '{}' exists, but is not a lto tape drive", name);
+                param_bail!("name", "Entry '{}' exists, but is not a lto tape drive", name);
             }
             config.sections.remove(&name);
         },
diff --git a/src/api2/config/media_pool.rs b/src/api2/config/media_pool.rs
index 7c3708c6..be4ff053 100644
--- a/src/api2/config/media_pool.rs
+++ b/src/api2/config/media_pool.rs
@@ -2,7 +2,7 @@ use anyhow::{bail, Error};
 use ::serde::{Deserialize, Serialize};
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, MediaPoolConfig, MediaPoolConfigUpdater, MEDIA_POOL_NAME_SCHEMA,
@@ -35,7 +35,7 @@ pub fn create_pool(
     let (mut section_config, _digest) = pbs_config::media_pool::config()?;
 
     if section_config.sections.get(&config.name).is_some() {
-        bail!("Media pool '{}' already exists", config.name);
+        param_bail!("name", "Media pool '{}' already exists", config.name);
     }
 
     section_config.set_data(&config.name, "pool", &config)?;
diff --git a/src/api2/config/remote.rs b/src/api2/config/remote.rs
index 1846deaa..89564e8f 100644
--- a/src/api2/config/remote.rs
+++ b/src/api2/config/remote.rs
@@ -7,7 +7,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{http_err, ApiMethod, Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_client::{HttpClient, HttpClientOptions};
 use pbs_api_types::{
@@ -95,7 +95,7 @@ pub fn create_remote(
     let (mut section_config, _digest) = pbs_config::remote::config()?;
 
     if section_config.sections.get(&name).is_some() {
-        bail!("remote '{}' already exists.", name);
+        param_bail!("name", "remote '{}' already exists.", name);
     }
 
     let remote = Remote { name: name.clone(), config, password };
@@ -257,7 +257,7 @@ pub fn delete_remote(name: String, digest: Option<String>) -> Result<(), Error>
     let job_list: Vec<SyncJobConfig>  = sync_jobs.convert_to_typed_array("sync")?;
     for job in job_list {
         if job.remote == name {
-            bail!("remote '{}' is used by sync job '{}' (datastore '{}')", name, job.id, job.store);
+            param_bail!("name", "remote '{}' is used by sync job '{}' (datastore '{}')", name, job.id, job.store);
         }
     }
 
diff --git a/src/api2/config/sync.rs b/src/api2/config/sync.rs
index 32983556..93584ecb 100644
--- a/src/api2/config/sync.rs
+++ b/src/api2/config/sync.rs
@@ -4,7 +4,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, SyncJobConfig, SyncJobConfigUpdater, JOB_ID_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
@@ -133,7 +133,7 @@ pub fn create_sync_job(
     let (mut section_config, _digest) = sync::config()?;
 
     if section_config.sections.get(&config.id).is_some() {
-        bail!("job '{}' already exists.", config.id);
+        param_bail!("id", "job '{}' already exists.", config.id);
     }
 
     section_config.set_data(&config.id, "sync", &config)?;
diff --git a/src/api2/config/tape_backup_job.rs b/src/api2/config/tape_backup_job.rs
index 21ac32f8..4d452039 100644
--- a/src/api2/config/tape_backup_job.rs
+++ b/src/api2/config/tape_backup_job.rs
@@ -4,7 +4,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, TapeBackupJobConfig, TapeBackupJobConfigUpdater,
@@ -77,7 +77,7 @@ pub fn create_tape_backup_job(
     let (mut config, _digest) = pbs_config::tape_job::config()?;
 
     if config.sections.get(&job.id).is_some() {
-        bail!("job '{}' already exists.", job.id);
+        param_bail!("id", "job '{}' already exists.", job.id);
     }
 
     config.set_data(&job.id, "backup", &job)?;
diff --git a/src/api2/config/tape_encryption_keys.rs b/src/api2/config/tape_encryption_keys.rs
index 25cc6cc0..910eb0ab 100644
--- a/src/api2/config/tape_encryption_keys.rs
+++ b/src/api2/config/tape_encryption_keys.rs
@@ -3,7 +3,7 @@ use serde_json::Value;
 use hex::FromHex;
 
 use proxmox_router::{ApiMethod, Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, Fingerprint, KeyInfo, Kdf,
@@ -112,7 +112,7 @@ pub fn change_passphrase(
     let kdf = kdf.unwrap_or_default();
 
     if let Kdf::None = kdf {
-        bail!("Please specify a key derivation function (none is not allowed here).");
+        param_bail!("kdf", format_err!("Please specify a key derivation function (none is not allowed here)."));
     }
 
     let _lock = open_backup_lockfile(TAPE_KEYS_LOCKFILE, None, true)?;
@@ -137,8 +137,8 @@ pub fn change_passphrase(
     }
 
     let (key, created, fingerprint) = match (force, &password) {
-        (true, Some(_)) => bail!("password is not allowed when using force"),
-        (false, None) => bail!("missing parameter: password"),
+        (true, Some(_)) => param_bail!("password", format_err!("password is not allowed when using force")),
+        (false, None) => param_bail!("password", format_err!("missing parameter: password")),
         (false, Some(pass)) => key_config.decrypt(&|| Ok(pass.as_bytes().to_vec()))?,
         (true, None) => {
                 let key = load_keys()?.0.get(&fingerprint).ok_or_else(|| {
@@ -195,7 +195,7 @@ pub fn create_key(
     let kdf = kdf.unwrap_or_default();
 
     if let Kdf::None = kdf {
-        bail!("Please specify a key derivation function (none is not allowed here).");
+        param_bail!("kdf", format_err!("Please specify a key derivation function (none is not allowed here)."));
     }
 
     let (key, mut key_config) = KeyConfig::new(password.as_bytes(), kdf)?;
diff --git a/src/api2/config/traffic_control.rs b/src/api2/config/traffic_control.rs
index f7f1cd65..f992157d 100644
--- a/src/api2/config/traffic_control.rs
+++ b/src/api2/config/traffic_control.rs
@@ -4,7 +4,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{ApiMethod, Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     TrafficControlRule, TrafficControlRuleUpdater,
@@ -62,7 +62,7 @@ pub fn create_traffic_control(config: TrafficControlRule) -> Result<(), Error> {
     let (mut section_config, _digest) = pbs_config::traffic_control::config()?;
 
     if section_config.sections.get(&config.name).is_some() {
-        bail!("traffic control rule '{}' already exists.", config.name);
+        param_bail!("name", "traffic control rule '{}' already exists.", config.name);
     }
 
     section_config.set_data(&config.name, "rule", &config)?;
diff --git a/src/api2/config/verify.rs b/src/api2/config/verify.rs
index 1431244b..2d7e9485 100644
--- a/src/api2/config/verify.rs
+++ b/src/api2/config/verify.rs
@@ -4,7 +4,7 @@ use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{Router, RpcEnvironment, Permission};
-use proxmox_schema::api;
+use proxmox_schema::{api, param_bail};
 
 use pbs_api_types::{
     Authid, VerificationJobConfig, VerificationJobConfigUpdater, JOB_ID_SCHEMA,
@@ -85,7 +85,7 @@ pub fn create_verification_job(
     let (mut section_config, _digest) = verify::config()?;
 
     if section_config.sections.get(&config.id).is_some() {
-        bail!("job '{}' already exists.", config.id);
+        param_bail!("id", "job '{}' already exists.", config.id);
     }
 
     section_config.set_data(&config.id, "verification", &config)?;
-- 
2.30.2





More information about the pbs-devel mailing list