1use std::time::Duration;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("{reason}")]
14 InvalidUrl {
15 url: String,
17 reason: String,
19 },
20
21 #[error("page load timed out after {}s", timeout.as_secs())]
23 Timeout {
24 url: String,
26 timeout: Duration,
28 },
29
30 #[error("address not allowed: {0}")]
32 AddressNotAllowed(String),
33
34 #[error("engine error: {0}")]
36 Engine(String),
37
38 #[error("JavaScript evaluation failed: {0}")]
40 JavaScript(String),
41
42 #[error("screenshot capture failed: {0}")]
44 Screenshot(String),
45
46 #[error(transparent)]
48 Extract(#[from] crate::extract::ExtractError),
49
50 #[error(transparent)]
52 Io(#[from] std::io::Error),
53
54 #[error("invalid glob pattern: {0}")]
56 InvalidGlob(#[from] globset::Error),
57}
58
59impl Error {
60 #[must_use]
62 pub fn is_timeout(&self) -> bool {
63 matches!(self, Self::Timeout { .. })
64 }
65
66 #[must_use]
68 pub fn is_network(&self) -> bool {
69 matches!(self, Self::Timeout { .. } | Self::AddressNotAllowed(_))
70 }
71
72 #[must_use]
74 pub fn url(&self) -> Option<&str> {
75 match self {
76 Self::InvalidUrl { url, .. } | Self::Timeout { url, .. } | Self::AddressNotAllowed(url) => Some(url),
77 _ => None,
78 }
79 }
80}
81
82#[allow(clippy::used_underscore_items)]
83const _: () = {
84 fn _assert<T: Send + Sync>() {}
85 fn _check() {
86 _assert::<Error>();
87 }
88};
89
90#[derive(Debug)]
92pub(crate) enum UrlError {
93 Invalid(String),
95 PrivateAddress(String),
97}
98
99impl std::fmt::Display for UrlError {
100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101 match self {
102 Self::Invalid(reason) => f.write_str(reason),
103 Self::PrivateAddress(host) => {
104 write!(f, "access to private/local addresses is not allowed: {host}")
105 }
106 }
107 }
108}
109
110pub(crate) fn map_url_error(url: &str, e: UrlError) -> Error {
111 match e {
112 UrlError::PrivateAddress(host) => Error::AddressNotAllowed(host),
113 UrlError::Invalid(reason) => Error::InvalidUrl {
114 url: url.into(),
115 reason,
116 },
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn timeout_is_timeout() {
126 let err = Error::Timeout {
127 url: "https://example.com".into(),
128 timeout: Duration::from_secs(30),
129 };
130 assert!(err.is_timeout());
131 assert!(err.is_network());
132 assert_eq!(err.url(), Some("https://example.com"));
133 }
134
135 #[test]
136 fn address_not_allowed_is_network() {
137 let err = Error::AddressNotAllowed("127.0.0.1".into());
138 assert!(!err.is_timeout());
139 assert!(err.is_network());
140 assert_eq!(err.url(), Some("127.0.0.1"));
141 }
142
143 #[test]
144 fn invalid_url_has_url() {
145 let err = Error::InvalidUrl {
146 url: "bad://url".into(),
147 reason: "scheme not allowed".into(),
148 };
149 assert!(!err.is_timeout());
150 assert!(!err.is_network());
151 assert_eq!(err.url(), Some("bad://url"));
152 }
153
154 #[test]
155 fn engine_error_has_no_url() {
156 let err = Error::Engine("crashed".into());
157 assert!(!err.is_timeout());
158 assert!(err.url().is_none());
159 }
160}