From aca61bf6a65d0084651b275f61dfa7b3ee148fd7 Mon Sep 17 00:00:00 2001 From: Colton Hurst Date: Wed, 18 Oct 2023 10:19:41 -0400 Subject: [PATCH] SM-874: Handle error cases on project deletes --- .../src/secrets_manager/projects/delete.rs | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/bitwarden/src/secrets_manager/projects/delete.rs b/crates/bitwarden/src/secrets_manager/projects/delete.rs index 55f792669..fcdb74697 100644 --- a/crates/bitwarden/src/secrets_manager/projects/delete.rs +++ b/crates/bitwarden/src/secrets_manager/projects/delete.rs @@ -39,14 +39,28 @@ impl ProjectsDeleteResponse { pub(crate) fn process_response( response: BulkDeleteResponseModelListResponseModel, ) -> Result { - Ok(ProjectsDeleteResponse { - data: response - .data - .unwrap_or_default() - .into_iter() - .map(ProjectDeleteResponse::process_response) - .collect::>()?, - }) + let mut successes = Vec::new(); + let mut failures = Vec::new(); + + for item in response.data.unwrap_or_default() { + match ProjectDeleteResponse::process_response(item) { + Ok(data) => { + successes.push(data); + } + Err(Error::ApiError(error)) => { + failures.extend_from_slice(&error); + } + Err(_) => { + unreachable!(); + } + } + } + + if failures.is_empty() { + Ok(ProjectsDeleteResponse { data: successes }) + } else { + Err(Error::ApiError(failures)) + } } } @@ -61,9 +75,11 @@ impl ProjectDeleteResponse { pub(crate) fn process_response( response: BulkDeleteResponseModel, ) -> Result { - Ok(ProjectDeleteResponse { - id: response.id.ok_or(Error::MissingFields)?, - error: response.error, - }) + let id = response.id.ok_or(Error::MissingFields)?; + + match response.error { + Some(error) => Err(Error::ApiError(vec![(id, error)])), + None => Ok(ProjectDeleteResponse { id, error: None }), + } } }