aws_config/
retry.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Retry configuration
7
8// Re-export from aws-smithy-types
9pub use aws_smithy_types::retry::ErrorKind;
10pub use aws_smithy_types::retry::ProvideErrorKind;
11pub use aws_smithy_types::retry::RetryConfig;
12pub use aws_smithy_types::retry::RetryConfigBuilder;
13pub use aws_smithy_types::retry::RetryKind;
14pub use aws_smithy_types::retry::RetryMode;
15
16/// Errors for retry configuration
17pub mod error {
18    use std::fmt;
19    use std::num::ParseIntError;
20
21    // Re-export from aws-smithy-types
22    pub use aws_smithy_types::retry::RetryModeParseError;
23
24    #[derive(Debug)]
25    pub(crate) enum RetryConfigErrorKind {
26        /// The configured retry mode wasn't recognized.
27        InvalidRetryMode {
28            /// Cause of the error.
29            source: RetryModeParseError,
30        },
31        /// Max attempts must be greater than zero.
32        MaxAttemptsMustNotBeZero,
33        /// The max attempts value couldn't be parsed to an integer.
34        FailedToParseMaxAttempts {
35            /// Cause of the error.
36            source: ParseIntError,
37        },
38    }
39
40    /// Failure to parse retry config from profile file or environment variable.
41    #[derive(Debug)]
42    pub struct RetryConfigError {
43        pub(crate) kind: RetryConfigErrorKind,
44    }
45
46    impl fmt::Display for RetryConfigError {
47        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48            use RetryConfigErrorKind::*;
49            match &self.kind {
50                InvalidRetryMode { .. } => {
51                    write!(f, "invalid retry configuration")
52                }
53                MaxAttemptsMustNotBeZero { .. } => {
54                    write!(f, "invalid configuration: It is invalid to set max attempts to 0. Unset it or set it to an integer greater than or equal to one.")
55                }
56                FailedToParseMaxAttempts { .. } => {
57                    write!(f, "failed to parse max attempts",)
58                }
59            }
60        }
61    }
62
63    impl std::error::Error for RetryConfigError {
64        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
65            use RetryConfigErrorKind::*;
66            match &self.kind {
67                InvalidRetryMode { source, .. } => Some(source),
68                FailedToParseMaxAttempts { source, .. } => Some(source),
69                MaxAttemptsMustNotBeZero { .. } => None,
70            }
71        }
72    }
73
74    impl From<RetryConfigErrorKind> for RetryConfigError {
75        fn from(kind: RetryConfigErrorKind) -> Self {
76            Self { kind }
77        }
78    }
79}