Skip to main content

rightkit_browser/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum BrowserError {
5    #[error("no Chrome, Chromium, or Edge executable found")]
6    ChromeNotFound,
7    /// Chrome for Testing was not found (or an explicit path is missing); carries what was
8    /// searched and how to install it.
9    #[error("Chrome for Testing not found: {0}")]
10    ChromeForTestingNotFound(String),
11    /// Windows: Chrome's sandbox cannot read the browser in `directory` (the access list
12    /// lacks read and execute for the AppContainer principals in `principals`), so its
13    /// network service would crash at startup and navigations would hang or abort. Nothing
14    /// was launched. `fix` is the command that grants the access; an installer that unpacks
15    /// Chrome into a directory outside `Program Files` runs it once after unpacking.
16    #[error(
17        "Chrome's sandbox cannot read the browser in {}: no read and execute access for {}. Grant it with: {fix}",
18        .directory.display(),
19        .principals.join(" and ")
20    )]
21    ChromeSandboxAccess {
22        directory: std::path::PathBuf,
23        principals: Vec<&'static str>,
24        fix: String,
25    },
26    #[error("launch failed: {0}")]
27    Launch(String),
28    #[error("profile error: {0}")]
29    Profile(String),
30    #[error("profile '{0}' is already in use by another session")]
31    ProfileInUse(String),
32    #[error("cdp error: {0}")]
33    Cdp(String),
34    #[error("stale or foreign ref '{0}'; observe again before acting")]
35    StaleRef(String),
36    #[error("no element matched '{0}'")]
37    NoElement(String),
38    #[error("selector '{0}' is ambiguous ({1} matches)")]
39    Ambiguous(String, u64),
40    #[error("element '{0}' is not visible or has no box")]
41    NotInteractable(String),
42    #[error("unknown page '{0}'")]
43    UnknownPage(String),
44    #[error("unknown key '{0}'")]
45    UnknownKey(String),
46    #[error("timed out: {0}")]
47    Timeout(String),
48    #[error("invalid argument: {0}")]
49    Invalid(String),
50    /// The caller's admission hook refused the effect before it happened.
51    #[error("denied before effect: {0}")]
52    Denied(String),
53    /// The network (SSRF/host) policy refused a URL before any request left the machine.
54    #[error("blocked by network policy: {0}")]
55    Blocked(crate::policy::PolicyDenial),
56    /// The user's consent receipt is missing, revoked, expired or does not cover the request.
57    #[error("consent: {0}")]
58    Consent(String),
59    /// The requested real-session mode does not exist for this browser.
60    #[error("unsupported: {0}")]
61    Unsupported(String),
62    #[error("io: {0}")]
63    Io(#[from] std::io::Error),
64}
65
66pub type Result<T> = std::result::Result<T, BrowserError>;
67
68pub(crate) fn cdp<E: std::fmt::Display>(e: E) -> BrowserError {
69    BrowserError::Cdp(e.to_string())
70}