diff --git a/native/native_rust_library/src/backup.rs b/native/native_rust_library/src/backup.rs --- a/native/native_rust_library/src/backup.rs +++ b/native/native_rust_library/src/backup.rs @@ -242,7 +242,11 @@ .map_err(|err| err.to_string()); let result = match latest_backup_id_response { - Ok(result) => result, + Ok(Some(result)) => result, + Ok(None) => { + string_callback("".to_string(), promise_id, "null".to_string()); + return; + } Err(error) => { string_callback(error, promise_id, "".to_string()); return; @@ -321,14 +325,22 @@ async fn download_latest_backup_info( user_identifier: String, -) -> Result> { +) -> Result, Box> { let backup_client = BackupClient::new(BACKUP_SOCKET_ADDR)?; let latest_backup_descriptor = BackupDescriptor::Latest { user_identifier }; - let backup_info_response = backup_client + let backup_info_response = match backup_client .download_backup_data(&latest_backup_descriptor, RequestedData::BackupInfo) - .await?; + .await + { + Ok(response) => response, + Err(err) if err.is_backup_not_found() => return Ok(None), + Err(err) if err.is_user_not_found() => { + return Err(Box::new(UserNotFoundError)) + } + Err(err) => return Err(err.into()), + }; let LatestBackupInfoResponse { backup_id, @@ -336,11 +348,11 @@ siwe_backup_msg, } = serde_json::from_slice(&backup_info_response)?; - Ok(LatestBackupInfoResponse { + Ok(Some(LatestBackupInfoResponse { backup_id, user_id, siwe_backup_msg, - }) + })) } async fn download_backup_keys( @@ -419,6 +431,11 @@ }) } +/// helper struct to generate proper error message string +#[derive(Debug, derive_more::Error, derive_more::Display)] +#[display(fmt = "user_not_found")] +struct UserNotFoundError; + // This struct should match `SIWEBackupData` in `lib/types/backup-types.js` #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] diff --git a/shared/backup_client/src/lib.rs b/shared/backup_client/src/lib.rs --- a/shared/backup_client/src/lib.rs +++ b/shared/backup_client/src/lib.rs @@ -139,7 +139,6 @@ let response = request.send().await?; let result = response.error_for_status()?.bytes().await?.to_vec(); - Ok(result) } } @@ -398,6 +397,17 @@ Unauthenticated, InvalidRequest, } + +impl Error { + pub fn is_user_not_found(&self) -> bool { + matches!(self, Self::ReqwestError(err) if err.status() == Some(StatusCode::BAD_REQUEST)) + } + + pub fn is_backup_not_found(&self) -> bool { + matches!(self, Self::ReqwestError(err) if err.status() == Some(StatusCode::NOT_FOUND)) + } +} + impl std::error::Error for Error {} impl From for Error {