Skip to main content

servo_fetch/
error.rs

1//! Error types.
2
3use std::time::Duration;
4
5/// A specialized `Result` type for servo-fetch.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors from servo-fetch operations.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// The URL is malformed or uses a disallowed scheme.
13    #[error("{reason}")]
14    InvalidUrl {
15        /// The URL that failed validation.
16        url: String,
17        /// Why the URL is invalid.
18        reason: String,
19    },
20
21    /// The page did not finish loading within the configured timeout.
22    #[error("page load timed out after {}s", timeout.as_secs())]
23    Timeout {
24        /// The URL that timed out.
25        url: String,
26        /// The timeout that was exceeded.
27        timeout: Duration,
28    },
29
30    /// The URL resolves to a private or reserved address (SSRF protection).
31    #[error("address not allowed: {0}")]
32    AddressNotAllowed(String),
33
34    /// The Servo engine is unavailable or crashed.
35    #[error("engine error: {0}")]
36    Engine(String),
37
38    /// JavaScript evaluation failed.
39    #[error("JavaScript evaluation failed: {0}")]
40    JavaScript(String),
41
42    /// Screenshot capture failed.
43    #[error("screenshot capture failed: {0}")]
44    Screenshot(String),
45
46    /// Content extraction failed.
47    #[error(transparent)]
48    Extract(#[from] crate::extract::ExtractError),
49
50    /// An I/O error occurred.
51    #[error(transparent)]
52    Io(#[from] std::io::Error),
53
54    /// A glob pattern is invalid.
55    #[error("invalid glob pattern: {0}")]
56    InvalidGlob(#[from] globset::Error),
57}
58
59impl Error {
60    /// Returns `true` if this is a timeout error.
61    #[must_use]
62    pub fn is_timeout(&self) -> bool {
63        matches!(self, Self::Timeout { .. })
64    }
65
66    /// Returns `true` if this is a network-related error.
67    #[must_use]
68    pub fn is_network(&self) -> bool {
69        matches!(self, Self::Timeout { .. } | Self::AddressNotAllowed(_))
70    }
71
72    /// Returns the URL associated with this error, if any.
73    #[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/// Why a URL was rejected by [`validate_url`].
91#[derive(Debug)]
92pub(crate) enum UrlError {
93    /// Malformed URL or disallowed scheme.
94    Invalid(String),
95    /// Host resolves to a private or reserved address.
96    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}