graphrt_provider_api/
error.rs1use std::fmt;
5use std::time::Duration;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub enum ProviderErrorKind {
13 Capacity,
16 RateLimited,
18 Auth,
20 NotFound,
22 Transient,
24 Fatal,
26 PlatformTimeout,
30}
31
32impl ProviderErrorKind {
33 pub fn is_retryable(self) -> bool {
35 matches!(self, Self::RateLimited | Self::Transient)
36 }
37
38 pub fn as_str(self) -> &'static str {
40 match self {
41 Self::Capacity => "capacity",
42 Self::RateLimited => "rate_limited",
43 Self::Auth => "auth",
44 Self::NotFound => "not_found",
45 Self::Transient => "transient",
46 Self::Fatal => "fatal",
47 Self::PlatformTimeout => "platform_timeout",
48 }
49 }
50}
51
52impl fmt::Display for ProviderErrorKind {
53 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54 formatter.write_str(self.as_str())
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
61#[error("{kind}: {message}")]
62pub struct ProviderError {
63 pub kind: ProviderErrorKind,
65 pub message: String,
67 pub retry_after: Option<Duration>,
69}
70
71impl ProviderError {
72 pub fn new(kind: ProviderErrorKind, message: impl Into<String>) -> Self {
74 Self { kind, message: message.into(), retry_after: None }
75 }
76
77 pub fn with_retry_after(mut self, retry_after: Option<Duration>) -> Self {
79 self.retry_after = retry_after;
80 self
81 }
82
83 pub fn capacity(message: impl Into<String>) -> Self {
85 Self::new(ProviderErrorKind::Capacity, message)
86 }
87 pub fn rate_limited(message: impl Into<String>) -> Self {
89 Self::new(ProviderErrorKind::RateLimited, message)
90 }
91 pub fn auth(message: impl Into<String>) -> Self {
93 Self::new(ProviderErrorKind::Auth, message)
94 }
95 pub fn not_found(message: impl Into<String>) -> Self {
97 Self::new(ProviderErrorKind::NotFound, message)
98 }
99 pub fn transient(message: impl Into<String>) -> Self {
101 Self::new(ProviderErrorKind::Transient, message)
102 }
103 pub fn fatal(message: impl Into<String>) -> Self {
105 Self::new(ProviderErrorKind::Fatal, message)
106 }
107 pub fn platform_timeout(message: impl Into<String>) -> Self {
109 Self::new(ProviderErrorKind::PlatformTimeout, message)
110 }
111
112 pub fn kind_for_status(status: u16) -> ProviderErrorKind {
116 match status {
117 401 | 403 => ProviderErrorKind::Auth,
118 404 | 410 => ProviderErrorKind::NotFound,
119 429 => ProviderErrorKind::RateLimited,
120 408 | 409 | 425 | 500..=599 => ProviderErrorKind::Transient,
121 _ => ProviderErrorKind::Fatal,
122 }
123 }
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
128pub enum DeployErrorKind {
129 Invalid,
131 Unsupported,
133 Provider(ProviderErrorKind),
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
139#[error("deploy failed ({kind:?}): {message}")]
140pub struct DeployError {
141 pub kind: DeployErrorKind,
143 pub message: String,
145}
146
147impl DeployError {
148 pub fn invalid(message: impl Into<String>) -> Self {
150 Self { kind: DeployErrorKind::Invalid, message: message.into() }
151 }
152 pub fn unsupported(message: impl Into<String>) -> Self {
154 Self { kind: DeployErrorKind::Unsupported, message: message.into() }
155 }
156}
157
158impl From<ProviderError> for DeployError {
159 fn from(error: ProviderError) -> Self {
160 Self { kind: DeployErrorKind::Provider(error.kind), message: error.message }
161 }
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167
168 #[test]
169 fn platform_timeout_is_its_own_non_retryable_kind() {
170 let error = ProviderError::platform_timeout("Lambda cut the run after 840 s");
171 assert_eq!(error.kind, ProviderErrorKind::PlatformTimeout);
172 assert!(!error.kind.is_retryable());
173 assert_eq!(error.kind.as_str(), "platform_timeout");
174 assert_eq!(error.to_string(), "platform_timeout: Lambda cut the run after 840 s");
175 assert_eq!(DeployError::from(error).kind, DeployErrorKind::Provider(ProviderErrorKind::PlatformTimeout));
176 }
177}