1use std::fmt;
4
5#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error("network error for {url}: {source}")]
11 Network {
12 url: String,
14 source: BoxError,
16 },
17
18 #[error("http {status} for {url}")]
20 Status {
21 url: String,
23 status: u16,
25 retry_after: Option<f64>,
27 },
28
29 #[error("body for {url} exceeds the {limit} byte cap")]
31 BodyTooLarge {
32 url: String,
34 limit: usize,
36 },
37
38 #[error("blocked by robots.txt: {0}")]
40 RobotsDenied(String),
41
42 #[error("invalid url {0}")]
44 InvalidUrl(String),
45
46 #[error("html parse error: {0}")]
48 Parse(String),
49
50 #[error("provider `{provider}` failed: {message}")]
52 Provider {
53 provider: String,
55 message: String,
57 },
58
59 #[error("headless browser fallback unavailable: {0}")]
61 Browser(String),
62
63 #[error("invalid configuration: {0}")]
65 Config(String),
66}
67
68pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
70
71pub type Result<T, E = Error> = std::result::Result<T, E>;
73
74impl Error {
75 pub(crate) fn network(url: impl fmt::Display, e: impl Into<BoxError>) -> Self {
76 Error::Network { url: url.to_string(), source: e.into() }
77 }
78
79 pub(crate) fn provider(provider: &'static str, message: impl fmt::Display) -> Self {
80 Error::Provider { provider: provider.to_string(), message: message.to_string() }
81 }
82
83 pub fn retry_after(&self) -> Option<std::time::Duration> {
85 match self {
86 Error::Status { retry_after: Some(secs), .. } if *secs >= 0.0 => {
87 Some(std::time::Duration::from_secs_f64(*secs))
88 }
89 _ => None,
90 }
91 }
92
93 pub fn is_retryable(&self) -> bool {
95 match self {
96 Error::Network { .. } => true,
97 Error::Status { status, .. } => {
98 matches!(status, 408 | 425 | 429 | 500 | 502 | 503 | 504)
99 }
100 _ => false,
101 }
102 }
103}