bitwarden_pam/access_rules/error.rs
1use bitwarden_core::{ApiError, MissingFieldError};
2use bitwarden_error::bitwarden_error;
3use reqwest::StatusCode;
4use thiserror::Error;
5
6use super::validate::AccessRuleValidationError;
7
8/// Errors returned from [`super::AccessRulesClient`] operations.
9#[bitwarden_error(flat)]
10#[derive(Debug, Error)]
11pub enum AccessRuleError {
12 /// The request failed local validation before being sent to the server.
13 #[error(transparent)]
14 Validation(#[from] AccessRuleValidationError),
15 /// The rule named by the request does not exist, or the caller cannot see it.
16 #[error("The access rule does not exist")]
17 NotFound,
18 /// The `conditions` field of a server response could not be interpreted.
19 #[error("Invalid conditions: {0}")]
20 InvalidConditions(String),
21 /// The server response was missing a field required to build the requested type.
22 #[error(transparent)]
23 MissingField(#[from] MissingFieldError),
24 /// A date field in the server response could not be parsed.
25 #[error(transparent)]
26 Chrono(#[from] chrono::ParseError),
27 /// A network or (de)serialization error occurred while calling the server.
28 #[error(transparent)]
29 Api(#[from] ApiError),
30}
31
32impl AccessRuleError {
33 /// Maps a `404` onto [`NotFound`](Self::NotFound) and every other failure onto
34 /// [`Api`](Self::Api).
35 ///
36 /// Only use this for calls that address one rule ([`get`](super::AccessRulesClient::get),
37 /// [`update`](super::AccessRulesClient::update), [`delete`](super::AccessRulesClient::delete)):
38 /// an org-scoped `404` says nothing about a rule.
39 pub(crate) fn from_api_error(error: ApiError) -> Self {
40 match &error {
41 ApiError::Response(content) if content.status == StatusCode::NOT_FOUND => {
42 Self::NotFound
43 }
44 _ => Self::Api(error),
45 }
46 }
47}