use viewpoint_cdp::protocol::runtime::{ExceptionDetails, ExceptionThrownEvent};
#[derive(Debug, Clone)]
pub struct PageError {
exception_details: ExceptionDetails,
timestamp: f64,
}
impl PageError {
pub(crate) fn from_event(event: ExceptionThrownEvent) -> Self {
Self {
exception_details: event.exception_details,
timestamp: event.timestamp,
}
}
pub fn message(&self) -> String {
if let Some(ref exception) = self.exception_details.exception {
if let Some(ref description) = exception.description {
return description.clone();
}
if let Some(ref value) = exception.value {
if let Some(s) = value.as_str() {
return s.to_string();
}
return value.to_string();
}
}
self.exception_details.text.clone()
}
pub fn stack(&self) -> Option<String> {
self.exception_details
.exception
.as_ref()
.and_then(|exc| exc.description.clone())
}
pub fn name(&self) -> Option<String> {
self.exception_details
.exception
.as_ref()
.and_then(|exc| exc.class_name.clone())
}
pub fn url(&self) -> Option<&str> {
self.exception_details.url.as_deref()
}
pub fn line_number(&self) -> i64 {
self.exception_details.line_number
}
pub fn column_number(&self) -> i64 {
self.exception_details.column_number
}
pub fn timestamp(&self) -> f64 {
self.timestamp
}
}
impl std::fmt::Display for PageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(name) = self.name() {
write!(f, "{}: {}", name, self.message())
} else {
write!(f, "{}", self.message())
}
}
}
impl std::error::Error for PageError {}
#[derive(Debug, Clone)]
pub struct WebError {
error: PageError,
target_id: String,
session_id: String,
}
impl WebError {
pub(crate) fn new(error: PageError, target_id: String, session_id: String) -> Self {
Self {
error,
target_id,
session_id,
}
}
pub fn message(&self) -> String {
self.error.message()
}
pub fn stack(&self) -> Option<String> {
self.error.stack()
}
pub fn name(&self) -> Option<String> {
self.error.name()
}
pub fn target_id(&self) -> &str {
&self.target_id
}
pub fn session_id(&self) -> &str {
&self.session_id
}
pub fn page_error(&self) -> &PageError {
&self.error
}
}
impl std::fmt::Display for WebError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}
impl std::error::Error for WebError {}