1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Error types for `aws-ssm-bridge`.
//!
//! Every fallible operation returns [`Result<T>`], aliasing
//! `std::result::Result<T, Error>`. [`Error`] is a flat enum: there is one
//! variant per failure domain and no nested error hierarchies to match through.
use std::fmt;
/// Result type alias for `aws-ssm-bridge` operations.
pub type Result<T> = std::result::Result<T, Error>;
/// The error type returned by every fallible operation in this crate.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// An AWS API call failed.
///
/// `code` is the typed service error code (e.g. `"ThrottlingException"`,
/// `"TargetNotConnected"`) when the SDK provided one. It is `None` for
/// transport-level failures (dispatch, timeout) and for errors this crate
/// constructs from a bare message.
#[error("AWS API error{}: {message}", .code.as_deref().map(|c| format!(" ({c})")).unwrap_or_default())]
Aws {
/// Human-readable description of the failure.
message: String,
/// Typed AWS service error code, when available.
code: Option<String>,
},
/// The SSM binary protocol was violated by the remote peer.
#[error("protocol error: {0}")]
Protocol(String),
/// The WebSocket transport failed or was closed.
#[error("transport error: {0}")]
Transport(String),
/// The session is not in a state that permits the requested operation.
#[error("session closed: {0}")]
SessionClosed(String),
/// Invalid configuration or arguments supplied by the caller.
#[error("invalid configuration: {0}")]
Config(String),
/// The SSM agent required a feature this client does not implement.
#[error("unsupported by this client: {0}")]
Unsupported(String),
/// Session encryption (KMS / AES-GCM) failed.
#[error("session encryption error: {0}")]
Crypto(String),
/// An underlying I/O operation failed.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// JSON (de)serialization failed.
#[error("serialization error: {0}")]
Serialization(#[from] serde_json::Error),
/// The operation did not complete within its deadline.
#[error("operation timed out after {0:?}")]
Timeout(std::time::Duration),
}
impl Error {
/// Construct an [`Error::Aws`] from a message with no typed service code.
pub(crate) fn aws(message: impl Into<String>) -> Self {
Error::Aws {
message: message.into(),
code: None,
}
}
/// Construct an [`Error::Protocol`].
pub(crate) fn protocol(message: impl Into<String>) -> Self {
Error::Protocol(message.into())
}
/// Construct an [`Error::Transport`].
pub(crate) fn transport(message: impl Into<String>) -> Self {
Error::Transport(message.into())
}
/// Whether retrying the same operation could plausibly succeed.
///
/// Transport failures and *transient* AWS service errors are retriable.
/// Permanent AWS errors (`AccessDeniedException`, `TargetNotConnected`,
/// `InvalidInstanceId`, …) are not, and neither are configuration or
/// protocol errors — retrying those just wastes the caller's time.
pub fn is_retriable(&self) -> bool {
match self {
Error::Timeout(_) | Error::Transport(_) => true,
Error::Io(e) => matches!(
e.kind(),
std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::ConnectionAborted
| std::io::ErrorKind::ConnectionRefused
| std::io::ErrorKind::BrokenPipe
| std::io::ErrorKind::TimedOut
| std::io::ErrorKind::NotConnected
| std::io::ErrorKind::Interrupted
),
Error::Aws { code, .. } => code.as_deref().is_some_and(is_transient_aws_code),
_ => false,
}
}
}
/// AWS service error codes that indicate a transient condition.
///
/// Sourced from the AWS SDK's standard retry classifier. Anything not listed
/// is treated as permanent: a client that retries `AccessDeniedException` is
/// broken, not resilient.
fn is_transient_aws_code(code: &str) -> bool {
matches!(
code,
"ThrottlingException"
| "Throttling"
| "ThrottledException"
| "TooManyRequestsException"
| "RequestThrottled"
| "RequestThrottledException"
| "ProvisionedThroughputExceededException"
| "TransactionInProgressException"
| "ServiceUnavailable"
| "ServiceUnavailableException"
| "InternalServerError"
| "InternalFailure"
| "RequestTimeout"
| "RequestTimeoutException"
)
}
impl<E, R> From<aws_smithy_runtime_api::client::result::SdkError<E, R>> for Error
where
E: fmt::Debug + aws_smithy_types::error::metadata::ProvideErrorMetadata,
R: fmt::Debug,
{
fn from(err: aws_smithy_runtime_api::client::result::SdkError<E, R>) -> Self {
use aws_smithy_types::error::metadata::ProvideErrorMetadata;
let code = err.code().map(str::to_owned);
// Prefer the service's own message; fall back to the Debug form, which
// carries dispatch/timeout detail that has no `message` field.
let message = err
.message()
.map(str::to_owned)
.unwrap_or_else(|| format!("{err:?}"));
Error::Aws { message, code }
}
}
#[cfg(test)]
mod tests {
use super::*;
fn aws_code(code: &str) -> Error {
Error::Aws {
message: "request failed".into(),
code: Some(code.into()),
}
}
#[test]
fn transient_aws_errors_are_retriable() {
assert!(aws_code("ThrottlingException").is_retriable());
assert!(aws_code("ServiceUnavailable").is_retriable());
assert!(aws_code("InternalFailure").is_retriable());
}
#[test]
fn permanent_aws_errors_are_not_retriable() {
assert!(!aws_code("AccessDeniedException").is_retriable());
assert!(!aws_code("TargetNotConnected").is_retriable());
assert!(!aws_code("InvalidInstanceId").is_retriable());
// No typed code at all: cannot prove it is transient, so do not retry.
assert!(!Error::aws("no code here").is_retriable());
}
#[test]
fn transport_and_timeout_are_retriable() {
assert!(Error::transport("reset by peer").is_retriable());
assert!(Error::Timeout(std::time::Duration::from_secs(1)).is_retriable());
assert!(!Error::Config("bad target".into()).is_retriable());
assert!(!Error::protocol("bad digest").is_retriable());
}
#[test]
fn aws_error_display_includes_code() {
let msg = aws_code("ThrottlingException").to_string();
assert!(msg.contains("ThrottlingException"), "{msg}");
assert!(msg.contains("request failed"), "{msg}");
}
}