Skip to main content

zendriver_imperva/
error.rs

1//! Imperva-bypass errors.
2
3use zendriver_interception::InterceptionError;
4use zendriver_transport::CallError;
5
6use crate::detection::CaptchaKind;
7
8/// Error returned by [`ImpervaBypass`] operations.
9///
10/// [`ImpervaBypass`]: crate::bypass::ImpervaBypass
11#[derive(Debug, thiserror::Error)]
12#[non_exhaustive]
13pub enum ImpervaError {
14    /// CAPTCHA detected, but no `on_captcha` solver was registered.
15    #[error("CAPTCHA required but no solver registered: {kind:?}")]
16    CaptchaRequired { kind: CaptchaKind },
17
18    /// User-supplied CAPTCHA solver returned an error.
19    #[error("CAPTCHA solver failed: {0}")]
20    CaptchaSolver(Box<dyn std::error::Error + Send + Sync>),
21
22    /// Fetch-domain interception hook failed at startup (only when
23    /// [`ImpervaBypass::with_interception`] was set).
24    ///
25    /// [`ImpervaBypass::with_interception`]: crate::bypass::ImpervaBypass::with_interception
26    #[error("interception hook error: {0}")]
27    Interception(#[from] InterceptionError),
28
29    /// CDP transport / call error.
30    #[error("call failed: {0}")]
31    Call(#[from] CallError),
32
33    /// In-page evaluator raised or returned an unexpected payload shape.
34    #[error("JS error: {0}")]
35    JsError(String),
36}
37
38#[cfg(test)]
39#[allow(clippy::panic, clippy::unwrap_used)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn display_captcha_required_includes_kind() {
45        let e = ImpervaError::CaptchaRequired {
46            kind: CaptchaKind::HCaptcha,
47        };
48        assert_eq!(
49            e.to_string(),
50            "CAPTCHA required but no solver registered: HCaptcha"
51        );
52    }
53
54    #[test]
55    fn display_js_error_passthrough() {
56        let e = ImpervaError::JsError("bad payload".into());
57        assert_eq!(e.to_string(), "JS error: bad payload");
58    }
59}