viewpoint_core/page/page_error/
mod.rs1use viewpoint_cdp::protocol::runtime::{ExceptionDetails, ExceptionThrownEvent};
7
8#[derive(Debug, Clone)]
22pub struct PageError {
23 exception_details: ExceptionDetails,
25 timestamp: f64,
27}
28
29impl PageError {
30 pub(crate) fn from_event(event: ExceptionThrownEvent) -> Self {
32 Self {
33 exception_details: event.exception_details,
34 timestamp: event.timestamp,
35 }
36 }
37
38 pub fn message(&self) -> String {
40 if let Some(ref exception) = self.exception_details.exception {
42 if let Some(ref description) = exception.description {
43 return description.clone();
44 }
45 if let Some(ref value) = exception.value {
46 if let Some(s) = value.as_str() {
47 return s.to_string();
48 }
49 return value.to_string();
50 }
51 }
52
53 self.exception_details.text.clone()
55 }
56
57 pub fn stack(&self) -> Option<String> {
59 self.exception_details.exception.as_ref().and_then(|exc| {
60 exc.description.clone()
61 })
62 }
63
64 pub fn name(&self) -> Option<String> {
66 self.exception_details.exception.as_ref().and_then(|exc| {
67 exc.class_name.clone()
68 })
69 }
70
71 pub fn url(&self) -> Option<&str> {
73 self.exception_details.url.as_deref()
74 }
75
76 pub fn line_number(&self) -> i64 {
78 self.exception_details.line_number
79 }
80
81 pub fn column_number(&self) -> i64 {
83 self.exception_details.column_number
84 }
85
86 pub fn timestamp(&self) -> f64 {
88 self.timestamp
89 }
90}
91
92impl std::fmt::Display for PageError {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 if let Some(name) = self.name() {
95 write!(f, "{}: {}", name, self.message())
96 } else {
97 write!(f, "{}", self.message())
98 }
99 }
100}
101
102impl std::error::Error for PageError {}
103
104#[derive(Debug, Clone)]
118pub struct WebError {
119 error: PageError,
121 target_id: String,
123 session_id: String,
125}
126
127impl WebError {
128 pub(crate) fn new(error: PageError, target_id: String, session_id: String) -> Self {
130 Self {
131 error,
132 target_id,
133 session_id,
134 }
135 }
136
137 pub fn message(&self) -> String {
139 self.error.message()
140 }
141
142 pub fn stack(&self) -> Option<String> {
144 self.error.stack()
145 }
146
147 pub fn name(&self) -> Option<String> {
149 self.error.name()
150 }
151
152 pub fn target_id(&self) -> &str {
154 &self.target_id
155 }
156
157 pub fn session_id(&self) -> &str {
159 &self.session_id
160 }
161
162 pub fn page_error(&self) -> &PageError {
164 &self.error
165 }
166}
167
168impl std::fmt::Display for WebError {
169 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170 write!(f, "{}", self.error)
171 }
172}
173
174impl std::error::Error for WebError {}