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    /// [`BrowserPage::act`](crate::BrowserPage::act) refused an action because the run repeated the same
37    /// action on the same ref with no effect. Cleared by `reset_action_watch`.
38    #[error("action loop stopped: {0}")]
39    ActionLoop(String),
40    #[error("no element matched '{0}'")]
41    NoElement(String),
42    #[error("selector '{0}' is ambiguous ({1} matches)")]
43    Ambiguous(String, u64),
44    #[error("element '{0}' is not visible or has no box")]
45    NotInteractable(String),
46    #[error("unknown page '{0}'")]
47    UnknownPage(String),
48    #[error("unknown key '{0}'")]
49    UnknownKey(String),
50    #[error("timed out: {0}")]
51    Timeout(String),
52    #[error("invalid argument: {0}")]
53    Invalid(String),
54    /// The caller's admission hook refused the effect before it happened.
55    #[error("denied before effect: {0}")]
56    Denied(String),
57    /// The network (SSRF/host) policy refused a URL before any request left the machine.
58    #[error("blocked by network policy: {0}")]
59    Blocked(crate::policy::PolicyDenial),
60    /// The user's consent receipt is missing, revoked, expired or does not cover the request.
61    #[error("consent: {0}")]
62    Consent(String),
63    /// The requested real-session mode does not exist for this browser.
64    #[error("unsupported: {0}")]
65    Unsupported(String),
66    /// The encrypted profile vault refused or failed (key unavailable, corrupt, in use,
67    /// plaintext left behind). See [`crate::VaultError`].
68    #[cfg(feature = "profile-vault")]
69    #[error("profile vault: {0}")]
70    Vault(#[from] crate::vault::VaultError),
71    /// Chrome's process tree did not stop cleanly when the session was closed.
72    #[error("browser did not stop cleanly: {0}")]
73    Stop(String),
74    #[error("io: {0}")]
75    Io(#[from] std::io::Error),
76}
77
78pub type Result<T> = std::result::Result<T, BrowserError>;
79
80pub(crate) fn cdp<E: std::fmt::Display>(e: E) -> BrowserError {
81    BrowserError::Cdp(e.to_string())
82}