Skip to main content

azure_pim_cli/
activate.rs

1use anyhow::{bail, Result};
2use reqwest::StatusCode;
3use serde_json::Value;
4use tracing::info;
5
6// NOTE: serde_json doesn't panic on failed index slicing, it returns a Value
7// that allows further nested nulls
8#[allow(clippy::indexing_slicing)]
9pub(crate) fn check_error_response(status: StatusCode, body: &Value) -> Result<()> {
10    if !status.is_success() {
11        if status == StatusCode::BAD_REQUEST {
12            if body["error"]["code"].as_str() == Some("RoleAssignmentExists") {
13                info!("role already assigned");
14                return Ok(());
15            }
16            if body["error"]["code"].as_str() == Some("RoleAssignmentRequestExists") {
17                info!("role assignment request already exists");
18                return Ok(());
19            }
20        }
21        bail!(
22            "request failed: status:{status:#?} body:{}",
23            serde_json::to_string_pretty(body)?
24        );
25    }
26    Ok(())
27}