Skip to main content

playwright_rs/
error.rs

1// Error types for playwright-core
2
3use thiserror::Error;
4
5/// Result type alias for playwright-core operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur when using playwright-core
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// Playwright server binary was not found
13    ///
14    /// The Playwright Node.js driver could not be located.
15    /// To resolve this, install Playwright using: `npm install playwright`
16    /// Or ensure the PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD environment variable is not set.
17    #[error("Playwright server not found. Install with: npm install playwright")]
18    ServerNotFound,
19
20    /// Failed to launch the Playwright server process
21    ///
22    /// The Playwright server process could not be started.
23    /// Common causes: Node.js not installed, insufficient permissions, or port already in use.
24    /// Details: {0}
25    #[error("Failed to launch Playwright server: {0}. Check that Node.js is installed.")]
26    LaunchFailed(String),
27
28    /// Server error (runtime issue with Playwright server)
29    #[error("Server error: {0}")]
30    ServerError(String),
31
32    /// Browser is not installed
33    ///
34    /// The specified browser has not been installed using Playwright's installation command.
35    /// To resolve this, install browsers using the versioned install command to ensure compatibility.
36    #[error(
37        "Browser '{browser_name}' is not installed.\n\n\
38        {message}\n\n\
39        To install {browser_name}, run:\n  \
40        npx playwright@{playwright_version} install {browser_name}\n\n\
41        Or install all browsers:\n  \
42        npx playwright@{playwright_version} install\n\n\
43        See: https://playwright.dev/docs/browsers"
44    )]
45    BrowserNotInstalled {
46        browser_name: String,
47        message: String,
48        playwright_version: String,
49    },
50
51    /// Failed to establish connection with the server
52    #[error("Failed to connect to Playwright server: {0}")]
53    ConnectionFailed(String),
54
55    /// Transport-level error (stdio communication)
56    #[error("Transport error: {0}")]
57    TransportError(String),
58
59    /// Protocol-level error (JSON-RPC)
60    #[error("Protocol error: {0}")]
61    ProtocolError(String),
62
63    /// I/O error
64    #[error("I/O error: {0}")]
65    Io(#[from] std::io::Error),
66
67    /// JSON serialization/deserialization error
68    #[error("JSON error: {0}")]
69    Json(#[from] serde_json::Error),
70
71    /// Timeout waiting for operation
72    ///
73    /// Contains context about what operation timed out and the timeout duration.
74    /// Common causes include slow network, server not responding, or element not becoming actionable.
75    /// Consider increasing the timeout or checking if the target is accessible.
76    #[error("Timeout: {0}")]
77    Timeout(String),
78
79    /// Navigation timeout
80    ///
81    /// Occurs when page navigation exceeds the specified timeout.
82    /// Includes the URL being navigated to and timeout duration.
83    #[error("Navigation timeout after {duration_ms}ms navigating to '{url}'")]
84    NavigationTimeout { url: String, duration_ms: u64 },
85
86    /// Target was closed (browser, context, or page)
87    ///
88    /// Occurs when attempting to perform an operation on a closed target.
89    /// The target must be recreated before it can be used again.
90    #[error("Target closed: Cannot perform operation on closed {target_type}. {context}")]
91    TargetClosed {
92        target_type: String,
93        context: String,
94    },
95
96    /// Unknown protocol object type
97    #[error("Unknown protocol object type: {0}")]
98    UnknownObjectType(String),
99
100    /// Channel closed unexpectedly
101    #[error("Channel closed unexpectedly")]
102    ChannelClosed,
103
104    /// Invalid argument provided to method
105    #[error("Invalid argument: {0}")]
106    InvalidArgument(String),
107
108    /// Element not found by selector
109    ///
110    /// Includes the selector that was used to locate the element.
111    /// This error typically occurs when waiting for an element times out.
112    #[error("Element not found: selector '{0}'")]
113    ElementNotFound(String),
114
115    /// Assertion timeout (expect API)
116    #[error("Assertion timeout: {0}")]
117    AssertionTimeout(String),
118
119    /// Assertion failed (expect API, server returned mismatch without timeout)
120    #[error("Assertion failed: {0}")]
121    AssertionFailed(String),
122    /// Object not found in registry (may have been closed/disposed)
123    #[error("Object not found (may have been closed): {0}")]
124    ObjectNotFound(String),
125
126    /// Type mismatch when downcasting a protocol object
127    ///
128    /// Occurs when a protocol object's concrete type does not match the expected type.
129    /// This typically indicates a Playwright protocol version mismatch or a bug in the
130    /// object factory.
131    #[error("Type mismatch for object '{guid}': expected {expected}, got {actual}")]
132    TypeMismatch {
133        guid: String,
134        expected: String,
135        actual: String,
136    },
137
138    /// Invalid path provided
139    #[error("Invalid path: {0}")]
140    InvalidPath(String),
141
142    /// Error with additional context
143    #[error("{0}: {1}")]
144    Context(String, #[source] Box<Error>),
145}
146
147impl Error {
148    /// Adds context to the error
149    pub fn context(self, msg: impl Into<String>) -> Self {
150        Error::Context(msg.into(), Box::new(self))
151    }
152}