use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SidewinderErrorKind {
HostUnreachable,
TransientFailure,
Unauthorized,
NotFound,
BadRequest,
UnexpectedStatus,
MalformedResponse,
InvalidTransaction,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SidewinderError {
pub kind: SidewinderErrorKind,
pub operation: String,
pub status: Option<u16>,
pub message: String,
}
impl std::fmt::Display for SidewinderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.status {
Some(status) => write!(
f,
"SidewinderError ({:?}): operation '{}' failed (HTTP {}): {}",
self.kind, self.operation, status, self.message
),
None => write!(
f,
"SidewinderError ({:?}): operation '{}' failed: {}",
self.kind, self.operation, self.message
),
}
}
}
impl std::error::Error for SidewinderError {}
impl SidewinderError {
pub fn unreachable(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::HostUnreachable,
operation: operation.to_string(),
status: None,
message: message.to_string(),
}
}
pub fn transient(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::TransientFailure,
operation: operation.to_string(),
status: None,
message: message.to_string(),
}
}
pub fn unauthorized(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::Unauthorized,
operation: operation.to_string(),
status: Some(401),
message: message.to_string(),
}
}
pub fn not_found(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::NotFound,
operation: operation.to_string(),
status: Some(404),
message: message.to_string(),
}
}
pub fn bad_request(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::BadRequest,
operation: operation.to_string(),
status: Some(400),
message: message.to_string(),
}
}
pub fn unexpected_status(operation: &str, status: u16, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::UnexpectedStatus,
operation: operation.to_string(),
status: Some(status),
message: message.to_string(),
}
}
pub fn malformed_response(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::MalformedResponse,
operation: operation.to_string(),
status: None,
message: message.to_string(),
}
}
pub fn invalid_transaction(operation: &str, message: &str) -> Self {
Self {
kind: SidewinderErrorKind::InvalidTransaction,
operation: operation.to_string(),
status: None,
message: message.to_string(),
}
}
pub fn looks_unreachable(message: &str) -> bool {
let s = message.to_lowercase();
s.contains("tcp connect error")
|| s.contains("connection refused")
|| s.contains("connection reset")
|| s.contains("timed out")
|| s.contains("timeout")
|| s.contains("dns error")
|| s.contains("host unreachable")
|| s.contains("error sending request")
}
}