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)]
10pub enum Error {
11 /// Playwright server binary was not found
12 ///
13 /// The Playwright Node.js driver could not be located.
14 /// To resolve this, install Playwright using: `npm install playwright`
15 /// Or ensure the PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD environment variable is not set.
16 #[error("Playwright server not found. Install with: npm install playwright")]
17 ServerNotFound,
18
19 /// Failed to launch the Playwright server process
20 ///
21 /// The Playwright server process could not be started.
22 /// Common causes: Node.js not installed, insufficient permissions, or port already in use.
23 /// Details: {0}
24 #[error("Failed to launch Playwright server: {0}. Check that Node.js is installed.")]
25 LaunchFailed(String),
26
27 /// Server error (runtime issue with Playwright server)
28 #[error("Server error: {0}")]
29 ServerError(String),
30
31 /// Failed to establish connection with the server
32 #[error("Failed to connect to Playwright server: {0}")]
33 ConnectionFailed(String),
34
35 /// Transport-level error (stdio communication)
36 #[error("Transport error: {0}")]
37 TransportError(String),
38
39 /// Protocol-level error (JSON-RPC)
40 #[error("Protocol error: {0}")]
41 ProtocolError(String),
42
43 /// I/O error
44 #[error("I/O error: {0}")]
45 Io(#[from] std::io::Error),
46
47 /// JSON serialization/deserialization error
48 #[error("JSON error: {0}")]
49 Json(#[from] serde_json::Error),
50
51 /// Timeout waiting for operation
52 ///
53 /// Contains context about what operation timed out and the timeout duration.
54 /// Common causes include slow network, server not responding, or element not becoming actionable.
55 /// Consider increasing the timeout or checking if the target is accessible.
56 #[error("Timeout: {0}")]
57 Timeout(String),
58
59 /// Navigation timeout
60 ///
61 /// Occurs when page navigation exceeds the specified timeout.
62 /// Includes the URL being navigated to and timeout duration.
63 #[error("Navigation timeout after {duration_ms}ms navigating to '{url}'")]
64 NavigationTimeout { url: String, duration_ms: u64 },
65
66 /// Target was closed (browser, context, or page)
67 ///
68 /// Occurs when attempting to perform an operation on a closed target.
69 /// The target must be recreated before it can be used again.
70 #[error("Target closed: Cannot perform operation on closed {target_type}. {context}")]
71 TargetClosed {
72 target_type: String,
73 context: String,
74 },
75
76 /// Unknown protocol object type
77 #[error("Unknown protocol object type: {0}")]
78 UnknownObjectType(String),
79
80 /// Channel closed unexpectedly
81 #[error("Channel closed unexpectedly")]
82 ChannelClosed,
83
84 /// Invalid argument provided to method
85 #[error("Invalid argument: {0}")]
86 InvalidArgument(String),
87
88 /// Element not found by selector
89 ///
90 /// Includes the selector that was used to locate the element.
91 /// This error typically occurs when waiting for an element times out.
92 #[error("Element not found: selector '{0}'")]
93 ElementNotFound(String),
94
95 /// Assertion timeout (expect API)
96 #[error("Assertion timeout: {0}")]
97 AssertionTimeout(String),
98 /// Object not found in registry (may have been closed/disposed)
99 #[error("Object not found (may have been closed): {0}")]
100 ObjectNotFound(String),
101
102 /// Invalid path provided
103 #[error("Invalid path: {0}")]
104 InvalidPath(String),
105
106 /// Error with additional context
107 #[error("{0}: {1}")]
108 Context(String, #[source] Box<Error>),
109}
110
111impl Error {
112 /// Adds context to the error
113 pub fn context(self, msg: impl Into<String>) -> Self {
114 Error::Context(msg.into(), Box::new(self))
115 }
116}