rustack_secretsmanager_model/
error.rs1use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum SecretsManagerErrorCode {
12 #[default]
14 DecryptionFailure,
15 EncryptionFailure,
17 InternalServiceError,
19 InvalidAction,
21 InvalidNextTokenException,
23 InvalidParameterException,
25 InvalidRequestException,
27 LimitExceededException,
29 MalformedPolicyDocumentException,
31 MissingAction,
33 PreconditionNotMetException,
35 PublicPolicyException,
37 ResourceExistsException,
39 ResourceNotFoundException,
41}
42
43impl SecretsManagerErrorCode {
44 #[must_use]
46 pub fn error_type(&self) -> &'static str {
47 self.as_str()
48 }
49
50 #[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 #[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#[derive(Debug)]
101pub struct SecretsManagerError {
102 pub code: SecretsManagerErrorCode,
104 pub message: String,
106 pub status_code: http::StatusCode,
108 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 #[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 #[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 #[must_use]
151 pub fn error_type(&self) -> &'static str {
152 self.code.error_type()
153 }
154
155 #[must_use]
157 pub fn internal_error(message: impl Into<String>) -> Self {
158 Self::with_message(SecretsManagerErrorCode::InternalServiceError, message)
159 }
160
161 #[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 #[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 #[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#[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}