Skip to main content

scrapling_browser/
error.rs

1//! Error types for the `scrapling-browser` crate.
2//!
3//! Every fallible operation in this crate returns [`Result<T>`], which is an alias for
4//! `std::result::Result<T, BrowserError>`. The [`BrowserError`] enum covers the major
5//! failure categories you can encounter during browser automation: Playwright driver
6//! errors, navigation failures, timeouts, page-pool capacity issues, configuration
7//! mistakes, and a catch-all `Other` variant.
8//!
9//! Playwright driver errors are automatically converted via the `From<playwright_rs::Error>`
10//! implementation, so the `?` operator works seamlessly in async code that calls into
11//! the underlying Playwright bindings.
12
13use std::fmt;
14
15/// Errors that can occur during browser automation operations.
16///
17/// Each variant carries a human-readable message describing what went wrong.
18/// When you match on this enum, the variant tells you the *category* of failure
19/// so you can decide whether to retry, reconfigure, or bail out.
20#[derive(Debug)]
21pub enum BrowserError {
22    /// A Playwright driver or protocol error.
23    ///
24    /// This variant is produced when the underlying Playwright Rust bindings return an
25    /// error -- for example, when the browser process crashes, a CDP message fails, or
26    /// the driver cannot be started. It is also the target of the automatic
27    /// `From<playwright_rs::Error>` conversion.
28    Playwright(String),
29
30    /// A page navigation failure.
31    ///
32    /// Returned when `page.goto()` fails, the page content cannot be extracted after
33    /// repeated retries, or the Cloudflare solver encounters an unrecoverable problem.
34    /// Check the inner message for details about which URL or step failed.
35    Navigation(String),
36
37    /// An operation exceeded its deadline.
38    ///
39    /// Typically raised when `wait_for_selector` does not find the expected element
40    /// within `timeout_ms`. You can increase the timeout in [`BrowserConfig`] or the
41    /// per-request [`FetchParams`] to give the page more time to render.
42    Timeout(String),
43
44    /// A page-pool capacity or lookup error.
45    ///
46    /// Returned when you try to register more pages than `max_pages` allows, or when
47    /// a page-index lookup fails. This usually means too many concurrent fetches are
48    /// in flight for the configured pool size.
49    PagePool(String),
50
51    /// An invalid or inconsistent configuration value.
52    ///
53    /// Raised during [`BrowserConfig::validate`] or [`StealthConfig::validate`] when
54    /// a field is out of range (e.g. `max_pages > 50`), mutually exclusive options are
55    /// both set (e.g. `proxy` and `proxy_rotator`), or a file path does not exist.
56    Config(String),
57
58    /// An uncategorised error.
59    ///
60    /// Catch-all for errors that do not fit neatly into the other categories.
61    /// Inspect the inner message for specifics.
62    Other(String),
63}
64
65impl fmt::Display for BrowserError {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            Self::Playwright(e) => write!(f, "playwright error: {e}"),
69            Self::Navigation(e) => write!(f, "navigation error: {e}"),
70            Self::Timeout(e) => write!(f, "timeout: {e}"),
71            Self::PagePool(e) => write!(f, "page pool error: {e}"),
72            Self::Config(e) => write!(f, "config error: {e}"),
73            Self::Other(e) => write!(f, "{e}"),
74        }
75    }
76}
77
78impl std::error::Error for BrowserError {}
79
80impl From<playwright_rs::Error> for BrowserError {
81    fn from(e: playwright_rs::Error) -> Self {
82        Self::Playwright(e.to_string())
83    }
84}
85
86/// Convenience alias used throughout the crate so that functions can return
87/// `Result<T>` without specifying the error type every time. Import this alias
88/// alongside [`BrowserError`] when writing code that calls into `scrapling-browser`.
89pub type Result<T> = std::result::Result<T, BrowserError>;