use crate::error::DataError;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq)]
pub enum MassiveError {
RateLimited { retry_after: Option<Duration> },
Disconnected { reason: String },
Auth { message: String },
Api { status: u16, message: String },
Http { message: String },
Deserialize { message: String, payload: String },
EnvVar { var: &'static str },
InvalidInput { message: String },
}
impl std::fmt::Display for MassiveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MassiveError::RateLimited { retry_after } => {
write!(f, "Massive rate limited")?;
if let Some(duration) = retry_after {
write!(f, " (retry after {:?})", duration)?;
}
Ok(())
}
MassiveError::Disconnected { reason } => {
write!(f, "Massive disconnected: {}", reason)
}
MassiveError::Auth { message } => {
write!(f, "Massive auth failed: {}", message)
}
MassiveError::Api { status, message } => {
write!(f, "Massive API error ({}): {}", status, message)
}
MassiveError::Http { message } => {
write!(f, "Massive HTTP error: {}", message)
}
MassiveError::Deserialize { message, payload } => {
let boundary = payload.floor_char_boundary(100);
let truncated = &payload[..boundary];
let ellipsis = if boundary < payload.len() { "..." } else { "" };
write!(
f,
"Massive deserialize error: {} (payload: {truncated}{ellipsis})",
message
)
}
MassiveError::EnvVar { var } => {
write!(f, "Massive environment variable not set: {}", var)
}
MassiveError::InvalidInput { message } => {
write!(f, "Massive invalid input: {}", message)
}
}
}
}
impl std::error::Error for MassiveError {}
impl From<MassiveError> for DataError {
fn from(err: MassiveError) -> Self {
DataError::Socket(err.to_string())
}
}
impl From<reqwest::Error> for MassiveError {
fn from(err: reqwest::Error) -> Self {
MassiveError::Http {
message: err.to_string(),
}
}
}
impl From<tokio_tungstenite::tungstenite::Error> for MassiveError {
fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
MassiveError::Disconnected {
reason: err.to_string(),
}
}
}