Skip to content

Commit f765fa1

Browse files
committed
chore: improve validation message and error message
1 parent 1e9ac4c commit f765fa1

File tree

10 files changed

+151
-120
lines changed

10 files changed

+151
-120
lines changed

Cargo.lock

Lines changed: 60 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ strip = true
1414
opt-level = 's'
1515

1616
[workspace.package]
17-
version = "0.8.9"
17+
version = "0.8.10"
1818
edition = "2021"
1919
repository = "https://github.com/ldclabs/ic-cose"
2020
keywords = ["config", "cbor", "canister", "icp", "encryption"]

src/ic_cose_canister/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ crate-type = ["cdylib"]
1616

1717
[dependencies]
1818
ic_cose_types = { path = "../ic_cose_types", version = "0.8" }
19-
candid = { workspace = true }
19+
candid = { workspace = true, features = ["value", "printer"] }
2020
ciborium = { workspace = true }
2121
hex = { workspace = true }
2222
ic-cdk = { workspace = true }

src/ic_cose_canister/src/api_admin.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use candid::Principal;
1+
use candid::{pretty::candid::value::pp_value, CandidType, IDLValue, Principal};
22
use ic_cose_types::validate_principals;
33
use ic_cose_types::{
44
types::namespace::{CreateNamespaceInput, NamespaceInfo},
@@ -98,7 +98,7 @@ fn validate_admin_add_managers(args: BTreeSet<Principal>) -> Result<(), String>
9898
#[ic_cdk::update]
9999
fn validate2_admin_add_managers(args: BTreeSet<Principal>) -> Result<String, String> {
100100
validate_principals(&args)?;
101-
Ok("ok".to_string())
101+
pretty_format(&args)
102102
}
103103

104104
#[ic_cdk::update]
@@ -110,7 +110,7 @@ fn validate_admin_remove_managers(args: BTreeSet<Principal>) -> Result<(), Strin
110110
#[ic_cdk::update]
111111
fn validate2_admin_remove_managers(args: BTreeSet<Principal>) -> Result<String, String> {
112112
validate_principals(&args)?;
113-
Ok("ok".to_string())
113+
pretty_format(&args)
114114
}
115115

116116
#[ic_cdk::update]
@@ -122,7 +122,7 @@ fn validate_admin_add_auditors(args: BTreeSet<Principal>) -> Result<(), String>
122122
#[ic_cdk::update]
123123
fn validate2_admin_add_auditors(args: BTreeSet<Principal>) -> Result<String, String> {
124124
validate_principals(&args)?;
125-
Ok("ok".to_string())
125+
pretty_format(&args)
126126
}
127127

128128
#[ic_cdk::update]
@@ -134,7 +134,7 @@ fn validate_admin_remove_auditors(args: BTreeSet<Principal>) -> Result<(), Strin
134134
#[ic_cdk::update]
135135
fn validate2_admin_remove_auditors(args: BTreeSet<Principal>) -> Result<String, String> {
136136
validate_principals(&args)?;
137-
Ok("ok".to_string())
137+
pretty_format(&args)
138138
}
139139

140140
#[ic_cdk::update]
@@ -143,8 +143,8 @@ fn validate_admin_add_allowed_apis(_args: BTreeSet<String>) -> Result<(), String
143143
}
144144

145145
#[ic_cdk::update]
146-
fn validate2_admin_add_allowed_apis(_args: BTreeSet<String>) -> Result<String, String> {
147-
Ok("ok".to_string())
146+
fn validate2_admin_add_allowed_apis(args: BTreeSet<String>) -> Result<String, String> {
147+
pretty_format(&args)
148148
}
149149

150150
#[ic_cdk::update]
@@ -153,6 +153,16 @@ fn validate_admin_remove_allowed_apis(_args: BTreeSet<String>) -> Result<(), Str
153153
}
154154

155155
#[ic_cdk::update]
156-
fn validate2_admin_remove_allowed_apis(_args: BTreeSet<String>) -> Result<String, String> {
157-
Ok("ok".to_string())
156+
fn validate2_admin_remove_allowed_apis(args: BTreeSet<String>) -> Result<String, String> {
157+
pretty_format(&args)
158+
}
159+
160+
fn pretty_format<T>(data: &T) -> Result<String, String>
161+
where
162+
T: CandidType,
163+
{
164+
let val = IDLValue::try_from_candid_type(data).map_err(|err| format!("{err:?}"))?;
165+
let doc = pp_value(7, &val);
166+
167+
Ok(format!("{}", doc.pretty(120)))
158168
}

src/ic_cose_canister/src/api_identity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn namespace_get_delegators(
3232
}
3333

3434
ns.fixed_id_names.get(&name).map_or_else(
35-
|| Err("name not found".to_string()),
35+
|| Err("NotFound: name not found".to_string()),
3636
|delegators| Ok(delegators.clone()),
3737
)
3838
})
@@ -99,7 +99,7 @@ fn namespace_sign_delegation(input: SignDelegationInput) -> Result<SignInRespons
9999
}
100100
return Err(format!("caller {} is not a delegator", caller));
101101
}
102-
Err("name not found".to_string())
102+
Err("NotFound: name not found".to_string())
103103
})?;
104104
if session_expires_in_ms == 0 {
105105
return Err("delegation is disabled".to_string());

src/ic_cose_canister/src/store.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ pub mod ns {
684684
NAMESPACES_STORE.with_borrow(|r| {
685685
r.get(namespace)
686686
.map(f)
687-
.unwrap_or_else(|| Err(format!("namespace {} not found", namespace)))
687+
.unwrap_or_else(|| Err(format!("NotFound: namespace {} not found", namespace)))
688688
})
689689
}
690690

@@ -700,7 +700,7 @@ pub mod ns {
700700
}
701701
Err(err) => Err(err),
702702
},
703-
None => Err(format!("namespace {} not found", namespace)),
703+
None => Err(format!("NotFound: namespace {} not found", namespace)),
704704
})
705705
}
706706

@@ -1055,7 +1055,7 @@ pub mod ns {
10551055
r.remove(&namespace);
10561056
Ok(())
10571057
}
1058-
None => Err(format!("namespace {} not found", namespace)),
1058+
None => Err(format!("NotFound: namespace {} not found", namespace)),
10591059
})
10601060
}
10611061

@@ -1076,14 +1076,14 @@ pub mod ns {
10761076

10771077
pub fn get_setting_info(caller: Principal, spk: SettingPathKey) -> Result<SettingInfo, String> {
10781078
let setting = try_get_setting(&caller, &spk)
1079-
.ok_or_else(|| format!("setting {} not found or no permission", spk))?;
1079+
.ok_or_else(|| format!("NotFound: setting {} not found or no permission", spk))?;
10801080

10811081
Ok(setting.into_info(spk.2, spk.3, false))
10821082
}
10831083

10841084
pub fn get_setting(caller: Principal, spk: SettingPathKey) -> Result<SettingInfo, String> {
10851085
let setting = try_get_setting(&caller, &spk)
1086-
.ok_or_else(|| format!("setting {} not found or no permission", &spk))?;
1086+
.ok_or_else(|| format!("NotFound: setting {} not found or no permission", &spk))?;
10871087

10881088
if spk.4 != 0 && spk.4 != setting.version {
10891089
Err("version mismatch".to_string())?;
@@ -1097,15 +1097,15 @@ pub mod ns {
10971097
spk: SettingPathKey,
10981098
) -> Result<SettingArchivedPayload, String> {
10991099
let setting = try_get_setting(&caller, &spk)
1100-
.ok_or_else(|| format!("setting {} not found or no permission", &spk))?;
1100+
.ok_or_else(|| format!("NotFound: setting {} not found or no permission", &spk))?;
11011101

11021102
if spk.4 == 0 || spk.4 >= setting.version {
11031103
Err("version mismatch".to_string())?;
11041104
};
11051105

11061106
let payload = PAYLOADS_STORE.with_borrow(|r| {
11071107
r.get(&spk)
1108-
.ok_or_else(|| format!("setting {} payload not found", &spk))
1108+
.ok_or_else(|| format!("NotFound: setting {} payload not found", &spk))
11091109
})?;
11101110

11111111
Ok(SettingArchivedPayload {
@@ -1220,7 +1220,7 @@ pub mod ns {
12201220
Err(err) => Err(err),
12211221
}
12221222
}
1223-
None => Err(format!("setting {} not found", &spk)),
1223+
None => Err(format!("NotFound: setting {} not found", &spk)),
12241224
})
12251225
})
12261226
}
@@ -1254,7 +1254,7 @@ pub mod ns {
12541254

12551255
Ok(())
12561256
}
1257-
None => Err(format!("setting {} not found", &spk)),
1257+
None => Err(format!("NotFound: setting {} not found", &spk)),
12581258
})
12591259
})
12601260
}
@@ -1332,7 +1332,7 @@ pub mod ns {
13321332
version: setting.version,
13331333
})
13341334
}
1335-
None => Err(format!("setting {} not found", &spk)),
1335+
None => Err(format!("NotFound: setting {} not found", &spk)),
13361336
})?;
13371337

13381338
ns.payload_bytes_total = ns.payload_bytes_total.saturating_add(size as u64);

src/ic_wasm_canister/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ crate-type = ["cdylib"]
1616

1717
[dependencies]
1818
ic_cose_types = { path = "../ic_cose_types", version = "0.8" }
19-
candid = { workspace = true }
19+
candid = { workspace = true, features = ["value", "printer"] }
2020
futures = { workspace = true }
2121
ciborium = { workspace = true }
2222
hex = { workspace = true }

src/ic_wasm_canister/src/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn get_wasm(hash: ByteArray<32>) -> Result<WasmInfo, String> {
2525
wasm: w.wasm,
2626
hash,
2727
})
28-
.ok_or_else(|| "wasm not found".to_string())
28+
.ok_or_else(|| "NotFound: wasm not found".to_string())
2929
}
3030

3131
#[ic_cdk::query]
@@ -47,7 +47,7 @@ async fn get_canister_status(
4747
if canister != self_id {
4848
store::state::with(|s| {
4949
if !s.deployed_list.contains_key(&canister) {
50-
return Err("canister not found".to_string());
50+
return Err("NotFound: canister not found".to_string());
5151
}
5252
Ok(())
5353
})?;

src/ic_wasm_canister/src/api_admin.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use candid::Principal;
1+
use candid::{pretty::candid::value::pp_value, CandidType, IDLArgs, IDLValue, Principal};
22
use ic_cdk::management_canister as mgt;
33
use ic_cose_types::{
44
format_error,
@@ -55,25 +55,25 @@ fn admin_remove_committers(args: BTreeSet<Principal>) -> Result<(), String> {
5555
#[ic_cdk::update]
5656
fn validate_admin_add_managers(args: BTreeSet<Principal>) -> Result<String, String> {
5757
validate_principals(&args)?;
58-
Ok("ok".to_string())
58+
pretty_format(&args)
5959
}
6060

6161
#[ic_cdk::update]
6262
fn validate_admin_remove_managers(args: BTreeSet<Principal>) -> Result<String, String> {
6363
validate_principals(&args)?;
64-
Ok("ok".to_string())
64+
pretty_format(&args)
6565
}
6666

6767
#[ic_cdk::update]
6868
fn validate_admin_add_committers(args: BTreeSet<Principal>) -> Result<String, String> {
6969
validate_principals(&args)?;
70-
Ok("ok".to_string())
70+
pretty_format(&args)
7171
}
7272

7373
#[ic_cdk::update]
7474
fn validate_admin_remove_committers(args: BTreeSet<Principal>) -> Result<String, String> {
7575
validate_principals(&args)?;
76-
Ok("ok".to_string())
76+
pretty_format(&args)
7777
}
7878

7979
#[ic_cdk::update(guard = "is_controller_or_manager_or_committer")]
@@ -95,14 +95,16 @@ async fn validate_admin_add_wasm(
9595
args: AddWasmInput,
9696
force_prev_hash: Option<ByteArray<32>>,
9797
) -> Result<String, String> {
98+
let rt = pretty_format(&(&args.name, &args.description, &force_prev_hash))?;
9899
store::wasm::add_wasm(
99100
ic_cdk::api::msg_caller(),
100101
ic_cdk::api::time() / MILLISECONDS,
101102
args,
102103
force_prev_hash,
103104
true,
104105
)?;
105-
Ok("ok".to_string())
106+
107+
Ok(rt)
106108
}
107109

108110
#[ic_cdk::update(guard = "is_controller")]
@@ -206,22 +208,26 @@ async fn admin_create_on(
206208
#[ic_cdk::update]
207209
fn validate_admin_create_canister(
208210
wasm_name: String,
209-
_settings: Option<mgt::CanisterSettings>,
210-
_args: Option<ByteBuf>,
211+
settings: Option<mgt::CanisterSettings>,
212+
args: Option<ByteBuf>,
211213
) -> Result<String, String> {
212214
let _ = store::wasm::get_latest(&wasm_name)?;
213-
Ok("ok".to_string())
215+
let args = IDLArgs::from_bytes(&args.unwrap_or_else(|| ByteBuf::from(EMPTY_CANDID_ARGS)))
216+
.map_err(|err| format!("Invalid args: {err}"))?;
217+
pretty_format(&(&wasm_name, &settings, &args.to_string()))
214218
}
215219

216220
#[ic_cdk::update]
217221
fn validate_admin_create_on(
218-
_subnet: Principal,
222+
subnet: Principal,
219223
wasm_name: String,
220-
_settings: Option<mgt::CanisterSettings>,
221-
_args: Option<ByteBuf>,
224+
settings: Option<mgt::CanisterSettings>,
225+
args: Option<ByteBuf>,
222226
) -> Result<String, String> {
223227
let _ = store::wasm::get_latest(&wasm_name)?;
224-
Ok("ok".to_string())
228+
let args = IDLArgs::from_bytes(&args.unwrap_or_else(|| ByteBuf::from(EMPTY_CANDID_ARGS)))
229+
.map_err(|err| format!("Invalid args: {err}"))?;
230+
pretty_format(&(&subnet, &wasm_name, &settings, &args.to_string()))
225231
}
226232

227233
#[ic_cdk::update(guard = "is_controller")]
@@ -304,6 +310,18 @@ async fn validate_admin_deploy(
304310
args: DeployWasmInput,
305311
ignore_prev_hash: Option<ByteArray<32>>,
306312
) -> Result<String, String> {
313+
let args_ = IDLArgs::from_bytes(
314+
&args
315+
.args
316+
.unwrap_or_else(|| ByteBuf::from(EMPTY_CANDID_ARGS)),
317+
)
318+
.map_err(|err| format!("Invalid args: {err}"))?;
319+
let rt = pretty_format(&(
320+
&args.name,
321+
&args.canister,
322+
&args_.to_string(),
323+
&ignore_prev_hash,
324+
))?;
307325
let info = mgt::canister_info(&mgt::CanisterInfoArgs {
308326
canister_id: args.canister,
309327
num_requested_changes: None,
@@ -340,12 +358,12 @@ async fn validate_admin_deploy(
340358
.unwrap_or_default()
341359
});
342360
let _ = store::wasm::get_wasm(&hash)
343-
.ok_or_else(|| format!("wasm not found: {}", hex::encode(hash.as_ref())))?;
361+
.ok_or_else(|| format!("NotFound: wasm not found: {}", hex::encode(hash.as_ref())))?;
344362
} else {
345363
store::wasm::next_version(prev_hash)?;
346364
}
347365

348-
Ok("ok".to_string())
366+
Ok(rt)
349367
}
350368

351369
#[ic_cdk::update(guard = "is_controller_or_manager")]
@@ -429,7 +447,7 @@ async fn admin_batch_topup() -> Result<u128, String> {
429447
async fn admin_update_canister_settings(args: mgt::UpdateSettingsArgs) -> Result<(), String> {
430448
store::state::with(|s| {
431449
if !s.deployed_list.contains_key(&args.canister_id) {
432-
return Err("canister not found".to_string());
450+
return Err("NotFound: canister not found".to_string());
433451
}
434452
Ok(())
435453
})?;
@@ -439,11 +457,13 @@ async fn admin_update_canister_settings(args: mgt::UpdateSettingsArgs) -> Result
439457

440458
#[ic_cdk::update]
441459
async fn validate_admin_batch_call(
442-
_canisters: BTreeSet<Principal>,
443-
_method: String,
444-
_args: Option<ByteBuf>,
460+
canisters: BTreeSet<Principal>,
461+
method: String,
462+
args: Option<ByteBuf>,
445463
) -> Result<String, String> {
446-
Ok("ok".to_string())
464+
let args = IDLArgs::from_bytes(&args.unwrap_or_else(|| ByteBuf::from(EMPTY_CANDID_ARGS)))
465+
.map_err(|err| format!("Invalid args: {err}"))?;
466+
pretty_format(&(&canisters, &method, &args.to_string()))
447467
}
448468

449469
#[ic_cdk::update]
@@ -457,9 +477,19 @@ async fn validate_admin_update_canister_settings(
457477
) -> Result<String, String> {
458478
store::state::with(|s| {
459479
if !s.deployed_list.contains_key(&args.canister_id) {
460-
return Err("canister not found".to_string());
480+
return Err("NotFound: canister not found".to_string());
461481
}
462482
Ok(())
463483
})?;
464-
Ok("ok".to_string())
484+
pretty_format(&args)
485+
}
486+
487+
fn pretty_format<T>(data: &T) -> Result<String, String>
488+
where
489+
T: CandidType,
490+
{
491+
let val = IDLValue::try_from_candid_type(data).map_err(|err| format!("{err:?}"))?;
492+
let doc = pp_value(7, &val);
493+
494+
Ok(format!("{}", doc.pretty(120)))
465495
}

0 commit comments

Comments
 (0)