From 4c427e0e2c0303dd118fd556c39e05b9addb9ca8 Mon Sep 17 00:00:00 2001 From: Frederik Rothenberger Date: Wed, 20 Dec 2023 16:50:32 +0100 Subject: [PATCH] Refactor authenticated OP helper to allow returning proper results This is a refactoring in preparation for API v2 not panicking anymore. It should apply to both, errors in the `authenticated_anchor_operation` wrapper related to authentication and bookkeeping as well as errors in the actual operation. --- src/internet_identity/src/main.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/internet_identity/src/main.rs b/src/internet_identity/src/main.rs index 6d44f401ec..93af19090a 100644 --- a/src/internet_identity/src/main.rs +++ b/src/internet_identity/src/main.rs @@ -87,6 +87,7 @@ fn verify_tentative_device( user_verification_code, ) }) + .unwrap_or_else(|err| err) } #[update] @@ -111,6 +112,7 @@ fn add(anchor_number: AnchorNumber, device_data: DeviceData) { authenticated_anchor_operation(anchor_number, |anchor| { Ok(((), anchor_management::add(anchor, device_data))) }) + .unwrap_or_else(|err| err) } #[update] @@ -122,6 +124,7 @@ fn update(anchor_number: AnchorNumber, device_key: DeviceKey, device_data: Devic anchor_management::update(anchor, device_key, device_data), )) }) + .unwrap_or_else(|err| err) } #[update] @@ -133,6 +136,7 @@ fn replace(anchor_number: AnchorNumber, device_key: DeviceKey, device_data: Devi anchor_management::replace(anchor_number, anchor, device_key, device_data), )) }) + .unwrap_or_else(|err| err) } #[update] @@ -144,6 +148,7 @@ fn remove(anchor_number: AnchorNumber, device_key: DeviceKey) { anchor_management::remove(anchor_number, anchor, device_key), )) }) + .unwrap_or_else(|err| err) } /// Returns all devices of the anchor (authentication and recovery) but no information about device registrations. @@ -457,16 +462,13 @@ fn authenticate_and_record_activity(anchor_number: AnchorNumber) -> Option( +fn authenticated_anchor_operation( anchor_number: AnchorNumber, - op: impl FnOnce(&mut Anchor) -> Result<(R, Operation), R>, -) -> R { + op: impl FnOnce(&mut Anchor) -> Result<(R, Operation), E>, +) -> Result { let Ok((mut anchor, device_key)) = check_authentication(anchor_number) else { trap(&format!("{} could not be authenticated.", caller())); }; @@ -482,9 +484,9 @@ fn authenticated_anchor_operation( match result { Ok((ret, operation)) => { post_operation_bookkeeping(anchor_number, operation); - ret + Ok(ret) } - Err(err) => err, + Err(err) => Err(err), } } @@ -602,8 +604,7 @@ mod v2_api { (), anchor_management::identity_metadata_replace(anchor, metadata), )) - }); - Ok(()) + }) } }