Skip to main content

stealthscraper_rs/
error.rs

1//! The crate's error type, covering the browser, proxy, challenge, and state layers.
2
3use thiserror::Error;
4
5/// The main error type for the `stealthscraper-rs` library.
6#[derive(Error, Debug)]
7pub enum Error {
8    /// An error occurred while generating or interacting with the underlying stealth browser.
9    #[error("Browser automation error: {0}")]
10    BrowserError(String),
11
12    /// An error during realistic mouse/keyboard interaction emulation.
13    #[error("Interaction emulation error: {0}")]
14    InteractionError(String),
15
16    /// An error occurred setting up or running the local TLS proxy.
17    #[error("Proxy initialization failed: {0}")]
18    ProxyBindFailed(#[from] std::io::Error),
19
20    /// An error occurred within the HTTP/TLS impersonation client.
21    #[error("HTTP client error: {0}")]
22    HttpClientError(#[from] wreq::Error),
23
24    /// A bot-protection challenge was detected but could not be solved.
25    #[error("Unsolved challenge: {0}")]
26    Challenge(String),
27
28    /// The persistent state store failed to read or write.
29    #[error("State store error: {0}")]
30    StateStore(String),
31
32    /// Missing or invalid configuration state.
33    #[error("Configuration error: {0}")]
34    ConfigError(String),
35
36    /// An error occurred while spawning or joining background tasks.
37    #[error("Internal join error: {0}")]
38    JoinError(String),
39
40    /// An error occurred during TLS configuration or handshake.
41    #[error("TLS error: {0}")]
42    TlsError(String),
43
44    /// A generic internal error.
45    #[error("Internal error: {0}")]
46    Internal(String),
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_error_display_formatting() {
55        let err = Error::BrowserError("Failed to launch".to_string());
56        assert_eq!(
57            err.to_string(),
58            "Browser automation error: Failed to launch"
59        );
60
61        let err2 = Error::ConfigError("Missing timeout".to_string());
62        assert_eq!(err2.to_string(), "Configuration error: Missing timeout");
63
64        let err3 = Error::Internal("Crash".to_string());
65        assert_eq!(err3.to_string(), "Internal error: Crash");
66
67        let err4 = Error::TlsError("handshake timeout".to_string());
68        assert_eq!(err4.to_string(), "TLS error: handshake timeout");
69    }
70}