use std::fmt;
pub struct RunnerError {
pub kind: RunnerErrorKind,
pub index: usize,
pub test: Option<String>,
}
impl RunnerError {
pub fn new(kind: RunnerErrorKind, index: usize) -> Self {
Self {
kind,
index,
test: None,
}
}
}
impl fmt::Debug for RunnerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?} on the command with index={}{}",
self.kind,
self.index,
self.test
.as_ref()
.map_or("".into(), |test| format!(" {}", test))
)
}
}
pub enum RunnerErrorKind {
#[cfg(feature = "fantoccini_backend")]
WebdriverError(fantoccini::error::CmdError),
#[cfg(feature = "thirtyfour_backend")]
WebdriverError(thirtyfour::error::WebDriverError),
BranchValidationError(String),
MismatchedType(String),
Url(url::ParseError),
Timeout(String),
AssertFailed {
lhs: String,
rhs: String,
},
TestNotFound(String),
}
impl std::fmt::Debug for RunnerErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BranchValidationError(err) => write!(f, "branching error {:?}", err),
Self::WebdriverError(err) => write!(f, "webdriver error {:?}", err),
Self::Url(e) => write!(f, "url construction error {}", e),
Self::MismatchedType(desc) => write!(f, "mismatched type {}", desc),
Self::Timeout(desc) => write!(f, "timeout {}", desc),
Self::AssertFailed { lhs, rhs } => write!(f, "assert failed {} == {}", lhs, rhs),
Self::TestNotFound(name) => write!(f, "A test {} wasn't found", name),
}
}
}
#[cfg(feature = "fantoccini_backend")]
impl From<fantoccini::error::CmdError> for RunnerErrorKind {
fn from(err: fantoccini::error::CmdError) -> Self {
RunnerErrorKind::WebdriverError(err)
}
}
#[cfg(feature = "thirtyfour_backend")]
impl From<thirtyfour::error::WebDriverError> for RunnerErrorKind {
fn from(err: thirtyfour::error::WebDriverError) -> Self {
RunnerErrorKind::WebdriverError(err)
}
}
impl From<url::ParseError> for RunnerErrorKind {
fn from(err: url::ParseError) -> Self {
RunnerErrorKind::Url(err)
}
}
pub enum ParseError {
FormatError(serde_json::Error),
LocatorFormatError(String),
TypeError(String),
ValidationError(String),
NotUniqTestName(usize, usize),
}
impl std::fmt::Debug for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FormatError(err) => write!(f, "parsing json error {:?}", err),
Self::LocatorFormatError(err) => write!(f, "locator has wrong format {:?}", err),
Self::TypeError(err) => write!(f, "unexpected type {:?}", err),
Self::ValidationError(err) => write!(f, "validation error {:?}", err),
Self::NotUniqTestName(t1, t2) => write!(
f,
"file contains a test with not uniq name; (test1={}, test2={})",
t1, t2
),
}
}
}