Skip to main content

playwright_rs/protocol/
web_error.rs

1// Copyright 2026 Paul Adamson
2// Licensed under the Apache License, Version 2.0
3//
4// WebError - plain data struct constructed from the pageError event params.
5//
6// WebError is NOT a ChannelOwner. It is constructed inline in
7// BrowserContext::on_event("pageError") when the context-level weberror event
8// is dispatched.
9//
10// See: <https://playwright.dev/docs/api/class-weberror>
11
12/// Represents an uncaught JavaScript exception thrown on any page in a browser context.
13///
14/// `WebError` is the context-level companion to the page-level `on_pageerror` event.
15/// It wraps the error message alongside an optional back-reference to the [`Page`](crate::protocol::Page)
16/// that threw the error.
17///
18/// # Example
19///
20/// ```ignore
21/// use playwright_rs::protocol::Playwright;
22///
23/// #[tokio::main]
24/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
25///     let playwright = Playwright::launch().await?;
26///     let browser = playwright.chromium().launch().await?;
27///     let context = browser.new_context().await?;
28///
29///     context
30///         .on_weberror(|web_error| async move {
31///             println!(
32///                 "Uncaught error on page {:?}: {}",
33///                 web_error.page().map(|p| p.url()),
34///                 web_error.error()
35///             );
36///             Ok(())
37///         })
38///         .await?;
39///
40///     let page = context.new_page().await?;
41///     page.goto("about:blank", None).await?;
42///     // Trigger an uncaught error asynchronously
43///     let _ = page
44///         .evaluate_expression("setTimeout(() => { throw new Error('boom') }, 0)")
45///         .await;
46///
47///     browser.close().await?;
48///     Ok(())
49/// }
50/// ```
51///
52/// See: <https://playwright.dev/docs/api/class-weberror>
53#[derive(Clone, Debug)]
54pub struct WebError {
55    /// The page that threw the error, if still available.
56    page: Option<crate::protocol::Page>,
57    /// The error message extracted from the uncaught exception.
58    error: String,
59}
60
61impl WebError {
62    /// Creates a new `WebError` from event params.
63    ///
64    /// Called by `BrowserContext::on_event("pageError")` when the context-level
65    /// weberror dispatch path fires.
66    pub(crate) fn new(error: String, page: Option<crate::protocol::Page>) -> Self {
67        Self { page, error }
68    }
69
70    /// Returns the page that produced this error, if available.
71    ///
72    /// May be `None` if the page has already been closed or the page reference
73    /// could not be resolved from the connection registry.
74    ///
75    /// See: <https://playwright.dev/docs/api/class-weberror#web-error-page>
76    pub fn page(&self) -> Option<&crate::protocol::Page> {
77        self.page.as_ref()
78    }
79
80    /// Returns the error message of the uncaught JavaScript exception.
81    ///
82    /// See: <https://playwright.dev/docs/api/class-weberror#web-error-error>
83    pub fn error(&self) -> &str {
84        &self.error
85    }
86}