Skip to main content

rustack_secretsmanager_model/
error.rs

1//! Auto-generated from AWS Secrets Manager Smithy model. DO NOT EDIT.
2//!
3//! Secrets Manager errors use JSON format with a `__type` field containing the
4//! short error type name (e.g., `ResourceNotFoundException`).
5
6use std::fmt;
7
8/// Well-known Secrets Manager error codes.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum SecretsManagerErrorCode {
12    /// DecryptionFailure error.
13    #[default]
14    DecryptionFailure,
15    /// EncryptionFailure error.
16    EncryptionFailure,
17    /// InternalServiceError error.
18    InternalServiceError,
19    /// InvalidAction error.
20    InvalidAction,
21    /// InvalidNextTokenException error.
22    InvalidNextTokenException,
23    /// InvalidParameterException error.
24    InvalidParameterException,
25    /// InvalidRequestException error.
26    InvalidRequestException,
27    /// LimitExceededException error.
28    LimitExceededException,
29    /// MalformedPolicyDocumentException error.
30    MalformedPolicyDocumentException,
31    /// MissingAction error.
32    MissingAction,
33    /// PreconditionNotMetException error.
34    PreconditionNotMetException,
35    /// PublicPolicyException error.
36    PublicPolicyException,
37    /// ResourceExistsException error.
38    ResourceExistsException,
39    /// ResourceNotFoundException error.
40    ResourceNotFoundException,
41}
42
43impl SecretsManagerErrorCode {
44    /// Returns the short error type string for the JSON `__type` field.
45    #[must_use]
46    pub fn error_type(&self) -> &'static str {
47        self.as_str()
48    }
49
50    /// Returns the short error code string.
51    #[must_use]
52    pub fn as_str(&self) -> &'static str {
53        match self {
54            Self::DecryptionFailure => "DecryptionFailure",
55            Self::EncryptionFailure => "EncryptionFailure",
56            Self::InternalServiceError => "InternalServiceError",
57            Self::InvalidAction => "InvalidAction",
58            Self::InvalidNextTokenException => "InvalidNextTokenException",
59            Self::InvalidParameterException => "InvalidParameterException",
60            Self::InvalidRequestException => "InvalidRequestException",
61            Self::LimitExceededException => "LimitExceededException",
62            Self::MalformedPolicyDocumentException => "MalformedPolicyDocumentException",
63            Self::MissingAction => "MissingAction",
64            Self::PreconditionNotMetException => "PreconditionNotMetException",
65            Self::PublicPolicyException => "PublicPolicyException",
66            Self::ResourceExistsException => "ResourceExistsException",
67            Self::ResourceNotFoundException => "ResourceNotFoundException",
68        }
69    }
70
71    /// Returns the default HTTP status code for this error.
72    #[must_use]
73    pub fn default_status_code(&self) -> http::StatusCode {
74        match self {
75            Self::DecryptionFailure
76            | Self::EncryptionFailure
77            | Self::InvalidAction
78            | Self::InvalidNextTokenException
79            | Self::InvalidParameterException
80            | Self::InvalidRequestException
81            | Self::LimitExceededException
82            | Self::MalformedPolicyDocumentException
83            | Self::MissingAction
84            | Self::PreconditionNotMetException
85            | Self::PublicPolicyException
86            | Self::ResourceExistsException
87            | Self::ResourceNotFoundException => http::StatusCode::BAD_REQUEST,
88            Self::InternalServiceError => http::StatusCode::INTERNAL_SERVER_ERROR,
89        }
90    }
91}
92
93impl fmt::Display for SecretsManagerErrorCode {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        f.write_str(self.as_str())
96    }
97}
98
99/// An Secrets Manager error response.
100#[derive(Debug)]
101pub struct SecretsManagerError {
102    /// The error code.
103    pub code: SecretsManagerErrorCode,
104    /// A human-readable error message.
105    pub message: String,
106    /// The HTTP status code.
107    pub status_code: http::StatusCode,
108    /// The underlying source error, if any.
109    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
110}
111
112impl fmt::Display for SecretsManagerError {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(f, "SecretsManagerError({}): {}", self.code, self.message)
115    }
116}
117
118impl std::error::Error for SecretsManagerError {
119    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
120        self.source
121            .as_ref()
122            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
123    }
124}
125
126impl SecretsManagerError {
127    /// Create a new `SecretsManagerError` from an error code.
128    #[must_use]
129    pub fn new(code: SecretsManagerErrorCode) -> Self {
130        Self {
131            status_code: code.default_status_code(),
132            message: code.as_str().to_owned(),
133            code,
134            source: None,
135        }
136    }
137
138    /// Create a new `SecretsManagerError` with a custom message.
139    #[must_use]
140    pub fn with_message(code: SecretsManagerErrorCode, message: impl Into<String>) -> Self {
141        Self {
142            status_code: code.default_status_code(),
143            message: message.into(),
144            code,
145            source: None,
146        }
147    }
148
149    /// Returns the `__type` string for the JSON error response.
150    #[must_use]
151    pub fn error_type(&self) -> &'static str {
152        self.code.error_type()
153    }
154
155    /// Internal error.
156    #[must_use]
157    pub fn internal_error(message: impl Into<String>) -> Self {
158        Self::with_message(SecretsManagerErrorCode::InternalServiceError, message)
159    }
160
161    /// Missing action header.
162    #[must_use]
163    pub fn missing_action() -> Self {
164        Self::with_message(
165            SecretsManagerErrorCode::MissingAction,
166            "Missing required header: X-Amz-Target",
167        )
168    }
169
170    /// Unknown operation.
171    #[must_use]
172    pub fn unknown_operation(target: &str) -> Self {
173        Self::with_message(
174            SecretsManagerErrorCode::InvalidAction,
175            format!("Operation {target} is not supported."),
176        )
177    }
178
179    /// Not implemented.
180    #[must_use]
181    pub fn not_implemented(operation: &str) -> Self {
182        Self::with_message(
183            SecretsManagerErrorCode::InternalServiceError,
184            format!("Operation {operation} is not yet implemented"),
185        )
186    }
187}
188
189/// Create an `SecretsManagerError` from an error code.
190///
191/// # Examples
192///
193/// ```ignore
194/// let err = secretsmanager_error!(DecryptionFailure);
195/// assert_eq!(err.code, SecretsManagerErrorCode::DecryptionFailure);
196/// ```
197#[macro_export]
198macro_rules! secretsmanager_error {
199    ($code:ident) => {
200        $crate::error::SecretsManagerError::new($crate::error::SecretsManagerErrorCode::$code)
201    };
202    ($code:ident, $msg:expr) => {
203        $crate::error::SecretsManagerError::with_message(
204            $crate::error::SecretsManagerErrorCode::$code,
205            $msg,
206        )
207    };
208}