use serde::Deserialize;
use std::fmt;
#[derive(Debug, Clone, thiserror::Error)]
pub struct BidiError {
pub command: String,
pub error: String,
pub message: String,
pub stacktrace: Option<String>,
}
impl BidiError {
pub fn is_session_ended(&self) -> bool {
matches!(
self.error.as_str(),
"invalid session id" | "session not created" | "unable to close browser"
)
}
pub fn is_no_such(&self) -> bool {
self.error.starts_with("no such")
}
}
impl fmt::Display for BidiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BiDi command {} failed: {} — {}", self.command, self.error, self.message)
}
}
impl From<BidiError> for crate::error::WebDriverError {
fn from(e: BidiError) -> Self {
crate::error::WebDriverError::FatalError(e.to_string())
}
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct BidiErrorEnvelope {
pub error: String,
pub message: String,
#[serde(default)]
pub stacktrace: Option<String>,
}
impl BidiErrorEnvelope {
pub(crate) fn into_error(self, command: impl Into<String>) -> BidiError {
BidiError {
command: command.into(),
error: self.error,
message: self.message,
stacktrace: self.stacktrace,
}
}
}