use std::fmt;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("network error for {url}: {source}")]
Network {
url: String,
source: BoxError,
},
#[error("http {status} for {url}")]
Status {
url: String,
status: u16,
retry_after: Option<f64>,
},
#[error("body for {url} exceeds the {limit} byte cap")]
BodyTooLarge {
url: String,
limit: usize,
},
#[error("blocked by robots.txt: {0}")]
RobotsDenied(String),
#[error("invalid url {0}")]
InvalidUrl(String),
#[error("html parse error: {0}")]
Parse(String),
#[error("provider `{provider}` failed: {message}")]
Provider {
provider: String,
message: String,
},
#[error("headless browser fallback unavailable: {0}")]
Browser(String),
#[error("invalid configuration: {0}")]
Config(String),
}
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
pub type Result<T, E = Error> = std::result::Result<T, E>;
impl Error {
pub(crate) fn network(url: impl fmt::Display, e: impl Into<BoxError>) -> Self {
Error::Network { url: url.to_string(), source: e.into() }
}
pub(crate) fn provider(provider: &'static str, message: impl fmt::Display) -> Self {
Error::Provider { provider: provider.to_string(), message: message.to_string() }
}
pub fn retry_after(&self) -> Option<std::time::Duration> {
match self {
Error::Status { retry_after: Some(secs), .. } if *secs >= 0.0 => {
Some(std::time::Duration::from_secs_f64(*secs))
}
_ => None,
}
}
pub fn is_retryable(&self) -> bool {
match self {
Error::Network { .. } => true,
Error::Status { status, .. } => {
matches!(status, 408 | 425 | 429 | 500 | 502 | 503 | 504)
}
_ => false,
}
}
}