use std::fmt;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ProviderErrorKind {
Capacity,
RateLimited,
Auth,
NotFound,
Transient,
Fatal,
PlatformTimeout,
}
impl ProviderErrorKind {
pub fn is_retryable(self) -> bool {
matches!(self, Self::RateLimited | Self::Transient)
}
pub fn as_str(self) -> &'static str {
match self {
Self::Capacity => "capacity",
Self::RateLimited => "rate_limited",
Self::Auth => "auth",
Self::NotFound => "not_found",
Self::Transient => "transient",
Self::Fatal => "fatal",
Self::PlatformTimeout => "platform_timeout",
}
}
}
impl fmt::Display for ProviderErrorKind {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{kind}: {message}")]
pub struct ProviderError {
pub kind: ProviderErrorKind,
pub message: String,
pub retry_after: Option<Duration>,
}
impl ProviderError {
pub fn new(kind: ProviderErrorKind, message: impl Into<String>) -> Self {
Self { kind, message: message.into(), retry_after: None }
}
pub fn with_retry_after(mut self, retry_after: Option<Duration>) -> Self {
self.retry_after = retry_after;
self
}
pub fn capacity(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::Capacity, message)
}
pub fn rate_limited(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::RateLimited, message)
}
pub fn auth(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::Auth, message)
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::NotFound, message)
}
pub fn transient(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::Transient, message)
}
pub fn fatal(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::Fatal, message)
}
pub fn platform_timeout(message: impl Into<String>) -> Self {
Self::new(ProviderErrorKind::PlatformTimeout, message)
}
pub fn kind_for_status(status: u16) -> ProviderErrorKind {
match status {
401 | 403 => ProviderErrorKind::Auth,
404 | 410 => ProviderErrorKind::NotFound,
429 => ProviderErrorKind::RateLimited,
408 | 409 | 425 | 500..=599 => ProviderErrorKind::Transient,
_ => ProviderErrorKind::Fatal,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeployErrorKind {
Invalid,
Unsupported,
Provider(ProviderErrorKind),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("deploy failed ({kind:?}): {message}")]
pub struct DeployError {
pub kind: DeployErrorKind,
pub message: String,
}
impl DeployError {
pub fn invalid(message: impl Into<String>) -> Self {
Self { kind: DeployErrorKind::Invalid, message: message.into() }
}
pub fn unsupported(message: impl Into<String>) -> Self {
Self { kind: DeployErrorKind::Unsupported, message: message.into() }
}
}
impl From<ProviderError> for DeployError {
fn from(error: ProviderError) -> Self {
Self { kind: DeployErrorKind::Provider(error.kind), message: error.message }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn platform_timeout_is_its_own_non_retryable_kind() {
let error = ProviderError::platform_timeout("Lambda cut the run after 840 s");
assert_eq!(error.kind, ProviderErrorKind::PlatformTimeout);
assert!(!error.kind.is_retryable());
assert_eq!(error.kind.as_str(), "platform_timeout");
assert_eq!(error.to_string(), "platform_timeout: Lambda cut the run after 840 s");
assert_eq!(DeployError::from(error).kind, DeployErrorKind::Provider(ProviderErrorKind::PlatformTimeout));
}
}