Skip to content

Commit

Permalink
SM-874: Handle error cases on secret deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonhurst committed Oct 18, 2023
1 parent b10fd14 commit 8f0885e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bitwarden_api_api::apis::Error as ApiError;
use bitwarden_api_identity::apis::Error as IdentityError;
use reqwest::StatusCode;
use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Error)]
pub enum Error {
Expand Down Expand Up @@ -46,6 +47,9 @@ pub enum Error {
#[error("Received error message from server: [{}] {}", .status, .message)]
ResponseContent { status: StatusCode, message: String },

#[error("Received error messages from the API: {0:?}")]
ApiError(Vec<(Uuid, String)>),

#[error("Internal error: {0}")]
Internal(&'static str),
}
Expand Down
43 changes: 31 additions & 12 deletions crates/bitwarden/src/secrets_manager/secrets/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,28 @@ impl SecretsDeleteResponse {
pub(crate) fn process_response(
response: BulkDeleteResponseModelListResponseModel,
) -> Result<SecretsDeleteResponse> {
Ok(SecretsDeleteResponse {
data: response
.data
.unwrap_or_default()
.into_iter()
.map(SecretDeleteResponse::process_response)
.collect::<Result<_, _>>()?,
})
let mut successes = Vec::new();
let mut failures = Vec::new();

for item in response.data.unwrap_or_default() {
match SecretDeleteResponse::process_response(item) {
Ok(data) => {
successes.push(data);
}
Err(Error::ApiError(error)) => {
failures.extend_from_slice(&error);
}
Err(_) => {
unreachable!();
}
}
}

if failures.is_empty() {
Ok(SecretsDeleteResponse { data: successes })
} else {
Err(Error::ApiError(failures))
}
}
}

Expand All @@ -61,9 +75,14 @@ impl SecretDeleteResponse {
pub(crate) fn process_response(
response: BulkDeleteResponseModel,
) -> Result<SecretDeleteResponse> {
Ok(SecretDeleteResponse {
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(SecretDeleteResponse {
id: id,
error: None,
}),
}
}
}

0 comments on commit 8f0885e

Please sign in to comment.