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/// ```no_run
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    /// Source location of the error (script URL, line, column), if reported.
60    location: Option<WebErrorLocation>,
61}
62
63/// Source location of an uncaught exception: the script URL, 1-based line, and
64/// 0-based column.
65#[derive(Clone, Debug)]
66#[non_exhaustive]
67pub struct WebErrorLocation {
68    /// URL of the script that threw.
69    pub url: String,
70    /// 1-based line number.
71    pub line: i32,
72    /// 0-based column number.
73    pub column: i32,
74}
75
76impl WebError {
77    /// Creates a new `WebError` from event params.
78    ///
79    /// Called by `BrowserContext::on_event("pageError")` when the context-level
80    /// weberror dispatch path fires.
81    pub(crate) fn new(
82        error: String,
83        page: Option<crate::protocol::Page>,
84        location: Option<WebErrorLocation>,
85    ) -> Self {
86        Self {
87            page,
88            error,
89            location,
90        }
91    }
92
93    /// Returns the source location of the uncaught exception, if reported
94    /// (script URL, line, column).
95    ///
96    /// See: <https://playwright.dev/docs/api/class-weberror#web-error-location>
97    pub fn location(&self) -> Option<&WebErrorLocation> {
98        self.location.as_ref()
99    }
100
101    /// Returns the page that produced this error, if available.
102    ///
103    /// May be `None` if the page has already been closed or the page reference
104    /// could not be resolved from the connection registry.
105    ///
106    /// See: <https://playwright.dev/docs/api/class-weberror#web-error-page>
107    pub fn page(&self) -> Option<&crate::protocol::Page> {
108        self.page.as_ref()
109    }
110
111    /// Returns the error message of the uncaught JavaScript exception.
112    ///
113    /// See: <https://playwright.dev/docs/api/class-weberror#web-error-error>
114    pub fn error(&self) -> &str {
115        &self.error
116    }
117}