Skip to main content

playwright_cdp/
error.rs

1//! Error types for the crate.
2
3use serde_json::Value;
4use thiserror::Error;
5
6/// Convenience alias used throughout the crate.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// The single error type for all operations.
10///
11/// Mirrors the shape of `playwright-rust`'s error enum, drops the driver-only
12/// variants, and adds CDP-specific ones (`CdpError`, `NotImplemented`, `Http`).
13#[derive(Debug, Error)]
14pub enum Error {
15    #[error("browser launch failed: {0}")]
16    LaunchFailed(String),
17
18    #[error("connection failed: {0}")]
19    ConnectionFailed(String),
20
21    #[error("transport error: {0}")]
22    TransportError(String),
23
24    #[error("protocol error: {0}")]
25    ProtocolError(String),
26
27    #[error(transparent)]
28    Io(#[from] std::io::Error),
29
30    #[error(transparent)]
31    Json(#[from] serde_json::Error),
32
33    #[error("timeout: {0}")]
34    Timeout(String),
35
36    #[error("navigation to {url} timed out after {duration_ms}ms")]
37    NavigationTimeout { url: String, duration_ms: u64 },
38
39    #[error("target closed: {target_type} ({context})")]
40    TargetClosed { target_type: String, context: String },
41
42    #[error("element not found: {0}")]
43    ElementNotFound(String),
44
45    #[error("channel closed")]
46    ChannelClosed,
47
48    #[error("invalid argument: {0}")]
49    InvalidArgument(String),
50
51    /// A CDP method returned an error response.
52    #[error("CDP error {code}: {message}")]
53    CdpError {
54        code: i64,
55        message: String,
56        #[allow(dead_code)]
57        data: Option<Value>,
58    },
59
60    /// A method that exists for API completeness but is not yet implemented.
61    #[error("not yet implemented: {0}")]
62    NotImplemented(&'static str),
63
64    #[error("http error: {0}")]
65    Http(String),
66
67    /// Wrap an existing error with additional context.
68    #[error("{0}: {1}")]
69    Context(String, Box<Error>),
70}
71
72impl Error {
73    /// Attach a human-readable context string, mirroring `playwright-rust`.
74    pub fn context(self, msg: impl Into<String>) -> Self {
75        Error::Context(msg.into(), Box::new(self))
76    }
77}