1use std::{fmt, net, time::Duration};
2
3use crate::tags::{ParseTagError, ParseTagsError};
4
5#[derive(Debug)]
6pub enum Error {
7 UnexpectedNoneValue {
8 entity: String,
9 },
10 SdkError(Box<dyn std::error::Error + Send>),
11 InvalidResponseError {
12 message: String,
13 },
14 MultipleMatches {
15 entity: String,
16 },
17 InvalidTag(ParseTagError),
18 InvalidTags(ParseTagsError),
19 RunInstancesEmptyResponse,
20 InstanceStopExceededMaxWait {
21 max_wait: Duration,
22 instance: super::InstanceId,
23 },
24 WaitError(Box<dyn std::error::Error + Send>),
25 RunInstanceNoCapacity,
26 InvalidTimestampError {
27 value: String,
28 message: String,
29 },
30}
31
32impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match *self {
35 Self::InvalidTag(ref inner) => {
36 write!(f, "{inner}")
37 }
38 Self::InvalidTags(ref inner) => {
39 write!(f, "{inner}")
40 }
41 Self::UnexpectedNoneValue { ref entity } => {
42 write!(f, "entity \"{entity}\" was empty")
43 }
44 Self::SdkError(ref e) => write!(f, "sdk error: {e}"),
45 Self::InvalidResponseError { ref message } => {
46 write!(f, "invalid api response: {message}")
47 }
48 Self::MultipleMatches { ref entity } => {
49 write!(f, "multiple matches for {entity} found")
50 }
51 Self::RunInstancesEmptyResponse => {
52 write!(f, "empty instance response of RunInstances")
53 }
54 Self::InstanceStopExceededMaxWait {
55 ref max_wait,
56 ref instance,
57 } => {
58 write!(
59 f,
60 "instance {instance} did not wait in {} seconds",
61 max_wait.as_secs()
62 )
63 }
64 Self::WaitError(ref e) => write!(f, "waiter error: {e}"),
65 Self::RunInstanceNoCapacity => {
66 write!(f, "no capacity for rnu instance operation")
67 }
68 Self::InvalidTimestampError {
69 ref value,
70 ref message,
71 } => {
72 write!(f, "failed parsing \"{value}\" as timestamp: {message}")
73 }
74 }
75 }
76}
77
78impl std::error::Error for Error {}
79
80impl<T: std::error::Error + Send + 'static> From<aws_sdk_ec2::error::SdkError<T>> for Error {
81 fn from(value: aws_sdk_ec2::error::SdkError<T>) -> Self {
82 Self::SdkError(Box::new(value))
83 }
84}
85
86impl From<aws_sdk_ec2::waiters::instance_stopped::WaitUntilInstanceStoppedError> for Error {
87 fn from(value: aws_sdk_ec2::waiters::instance_stopped::WaitUntilInstanceStoppedError) -> Self {
88 Self::WaitError(Box::new(value))
89 }
90}
91
92impl From<net::AddrParseError> for Error {
93 fn from(value: net::AddrParseError) -> Self {
94 Self::InvalidResponseError {
95 message: value.to_string(),
96 }
97 }
98}
99
100impl From<ParseTagError> for Error {
101 fn from(value: ParseTagError) -> Self {
102 Self::InvalidTag(value)
103 }
104}
105
106impl From<ParseTagsError> for Error {
107 fn from(value: ParseTagsError) -> Self {
108 Self::InvalidTags(value)
109 }
110}