aws_smithy_runtime_api/client/
waiters.rs1pub mod error {
8 use crate::client::{
9 orchestrator::HttpResponse,
10 result::{ConstructionFailure, SdkError},
11 };
12 use crate::{box_error::BoxError, client::waiters::FinalPoll};
13 use aws_smithy_types::error::{
14 metadata::{ProvideErrorMetadata, EMPTY_ERROR_METADATA},
15 ErrorMetadata,
16 };
17 use std::{fmt, time::Duration};
18
19 #[derive(Debug)]
24 #[non_exhaustive]
25 #[allow(clippy::large_enum_variant)] pub enum WaiterError<O, E> {
27 ConstructionFailure(ConstructionFailure),
31
32 ExceededMaxWait(ExceededMaxWait),
34
35 FailureState(FailureState<O, E>),
44
45 OperationFailed(OperationFailed<E>),
53 }
54
55 impl<O, E> WaiterError<O, E> {
56 pub fn construction_failure(source: impl Into<BoxError>) -> Self {
58 Self::ConstructionFailure(ConstructionFailure::builder().source(source).build())
59 }
60 }
61
62 impl<O, E> std::error::Error for WaiterError<O, E>
63 where
64 O: fmt::Debug,
65 E: std::error::Error + fmt::Debug + 'static,
66 {
67 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
68 match self {
69 Self::ConstructionFailure(inner) => Some(&*inner.source),
70 Self::ExceededMaxWait(_) => None,
71 Self::FailureState(inner) => match &inner.final_poll.result {
72 Ok(_) => None,
73 Err(err) => Some(err),
74 },
75 Self::OperationFailed(inner) => Some(&inner.source),
76 }
77 }
78 }
79
80 impl<O, E> fmt::Display for WaiterError<O, E> {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match self {
83 Self::ConstructionFailure(_) => f.write_str("failed to construct waiter"),
84 Self::ExceededMaxWait(ctx) => {
85 write!(f, "exceeded max wait time ({:?})", ctx.max_wait)
86 }
87 Self::FailureState(_) => f.write_str("waiting failed"),
88 Self::OperationFailed(_) => f.write_str("operation failed while waiting"),
89 }
90 }
91 }
92
93 impl<O, E> ProvideErrorMetadata for WaiterError<O, E>
95 where
96 E: ProvideErrorMetadata,
97 {
98 fn meta(&self) -> &ErrorMetadata {
99 match self {
100 WaiterError::ConstructionFailure(_) | WaiterError::ExceededMaxWait(_) => {
101 &EMPTY_ERROR_METADATA
102 }
103 WaiterError::FailureState(inner) => inner
104 .final_poll()
105 .as_result()
106 .err()
107 .map(ProvideErrorMetadata::meta)
108 .unwrap_or(&EMPTY_ERROR_METADATA),
109 WaiterError::OperationFailed(inner) => inner.error().meta(),
110 }
111 }
112 }
113
114 #[derive(Debug)]
116 pub struct ExceededMaxWait {
117 max_wait: Duration,
118 elapsed: Duration,
119 poll_count: u32,
120 }
121
122 impl ExceededMaxWait {
123 pub fn new(max_wait: Duration, elapsed: Duration, poll_count: u32) -> Self {
125 Self {
126 max_wait,
127 elapsed,
128 poll_count,
129 }
130 }
131
132 pub fn max_wait(&self) -> Duration {
134 self.max_wait
135 }
136
137 pub fn elapsed(&self) -> Duration {
139 self.elapsed
140 }
141
142 pub fn poll_count(&self) -> u32 {
144 self.poll_count
145 }
146 }
147
148 #[derive(Debug)]
150 #[non_exhaustive]
151 pub struct FailureState<O, E> {
152 final_poll: FinalPoll<O, E>,
153 }
154
155 impl<O, E> FailureState<O, E> {
156 pub fn new(final_poll: FinalPoll<O, E>) -> Self {
158 Self { final_poll }
159 }
160
161 pub fn final_poll(&self) -> &FinalPoll<O, E> {
163 &self.final_poll
164 }
165
166 pub fn into_final_poll(self) -> FinalPoll<O, E> {
168 self.final_poll
169 }
170 }
171
172 #[derive(Debug)]
174 #[non_exhaustive]
175 pub struct OperationFailed<E> {
176 source: SdkError<E, HttpResponse>,
177 }
178
179 impl<E> OperationFailed<E> {
180 pub fn new(source: SdkError<E, HttpResponse>) -> Self {
182 Self { source }
183 }
184
185 pub fn error(&self) -> &SdkError<E, HttpResponse> {
187 &self.source
188 }
189
190 pub fn into_error(self) -> SdkError<E, HttpResponse> {
192 self.source
193 }
194 }
195}
196
197#[non_exhaustive]
203#[derive(Debug)]
204pub struct FinalPoll<O, E> {
205 result: Result<O, E>,
206}
207
208impl<O, E> FinalPoll<O, E> {
209 pub fn new(result: Result<O, E>) -> Self {
211 Self { result }
212 }
213
214 pub fn into_result(self) -> Result<O, E> {
216 self.result
217 }
218
219 pub fn as_result(&self) -> Result<&O, &E> {
221 self.result.as_ref()
222 }
223
224 pub fn map<O2, F: FnOnce(O) -> O2>(self, mapper: F) -> FinalPoll<O2, E> {
226 FinalPoll::new(self.result.map(mapper))
227 }
228
229 pub fn map_err<E2, F: FnOnce(E) -> E2>(self, mapper: F) -> FinalPoll<O, E2> {
231 FinalPoll::new(self.result.map_err(mapper))
232 }
233}