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