use super::*;
use std::error::Error as StdError;
fn io() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "boom")
}
fn receipt(dispatch: crate::DispatchState) -> crate::ActionReceipt {
crate::ActionReceipt {
destination_target_id: None,
started_at_ms: 0,
finished_at_ms: 0,
download_guid: None,
action: "click".into(),
target_id: "t".into(),
outcome: crate::ActionOutcome::NotDispatched,
dispatch,
preparation_may_have_executed: false,
verification: None,
elapsed_ms: 0,
already_satisfied: false,
cleanup_pending: false,
}
}
fn display_cases() -> Vec<(Error, &'static str, ErrorCode, bool)> {
let serde_err = serde_json::from_str::<u8>("x").unwrap_err();
vec![
(
Error::coded(ErrorCode::HistoryUnavailable, "no entry"),
"history_unavailable: no entry",
ErrorCode::HistoryUnavailable,
false,
),
(
Error::action(
ErrorCode::TargetUnavailable,
receipt(crate::DispatchState::NotSent),
Error::element_not_found("#a"),
),
"Action target_unavailable: Element not found: #a",
ErrorCode::TargetUnavailable,
true,
),
(
Error::transport("ctx"),
"Transport error: ctx",
ErrorCode::Transport,
true,
),
(
Error::transport_io("ctx", io()),
"Transport error: ctx: boom",
ErrorCode::Transport,
true,
),
(
Error::cdp("M.x", -1, "boom"),
"CDP error in M.x: boom (code -1)",
ErrorCode::CdpError,
true,
),
(
Error::cdp_parts(Some("M.x".into()), None, "boom"),
"CDP error in M.x: boom",
ErrorCode::CdpError,
true,
),
(
Error::cdp_parts(None, Some(-32001), "boom"),
"CDP error: boom",
ErrorCode::CdpError,
true,
),
(Error::cdp_msg("boom"), "CDP error: boom", ErrorCode::CdpError, true),
(
Error::element_not_found("#a"),
"Element not found: #a",
ErrorCode::ElementNotFound,
true,
),
(
Error::element_not_visible("#a"),
"Element not visible: '#a' exists in DOM but is not rendered (hidden, display:none, or off-screen)",
ErrorCode::ElementNotVisible,
true,
),
(
Error::invalid_url("bad"),
"Invalid URL: bad",
ErrorCode::InvalidUrl,
false,
),
(
Error::value_mismatch("#p", 8, 6),
"Value mismatch for #p: the control holds 6 characters after filling 8",
ErrorCode::ValueMismatch,
false,
),
(
Error::from(serde_err),
"Serialization error: expected value at line 1 column 1",
ErrorCode::Serialization,
false,
),
(
Error::too_large(7),
"Evaluation result exceeds its JSON byte limit of 7",
ErrorCode::ResultTooLarge,
false,
),
(Error::from(io()), "IO error: boom", ErrorCode::Io, true),
(
Error::chrome_not_found(),
"Chrome not found",
ErrorCode::ChromeNotFound,
false,
),
(
Error::patching("scan", "bad elf"),
"Patching error in scan: bad elf",
ErrorCode::Patching,
true,
),
(
Error::retry_exhausted(3, Error::timeout("slow")),
"Retry exhausted after 3 attempts: timeout: slow",
ErrorCode::RetryExhausted,
false,
),
(
Error::retry_exhausted(
2,
Error::action(
ErrorCode::ActionTimeout,
receipt(crate::DispatchState::NotSent),
Error::timeout("late"),
),
),
"Retry exhausted after 2 attempts: Action action_timeout: timeout: late",
ErrorCode::RetryExhausted,
false,
),
]
}
#[test]
fn display_matches_every_former_variant_byte_for_byte() {
for (error, expected, code, retryable) in display_cases() {
assert_eq!(error.to_string(), expected);
assert_eq!(error.clone().to_string(), expected, "clone of {expected}");
assert_eq!(error.code(), code, "{expected}");
assert_eq!(error.is_retryable(), retryable, "{expected}");
}
}
#[test]
fn is_retryable_follows_transience_and_dispatch() {
assert!(!Error::coded(ErrorCode::AmbiguousTarget, "x").is_retryable());
assert!(Error::timeout("x").is_retryable());
assert!(Error::launch("x").is_retryable());
assert!(Error::decode("x").is_retryable());
assert!(Error::coded(ErrorCode::NavigationFailed, "x").is_retryable());
assert!(Error::coded(ErrorCode::ActionabilityChanged, "x").is_retryable());
assert!(!Error::input_state("x").is_retryable());
assert!(!Error::invalid_argument("x").is_retryable());
let sent = Error::action(
ErrorCode::TargetUnavailable,
receipt(crate::DispatchState::MayHaveBeenSent),
Error::element_not_found("#a"),
);
assert!(!sent.is_retryable());
let permanent = Error::action(
ErrorCode::AmbiguousTarget,
receipt(crate::DispatchState::NotSent),
Error::coded(ErrorCode::AmbiguousTarget, "x"),
);
assert!(!permanent.is_retryable());
}
#[test]
fn accessors_expose_receipt_cause_cdp_and_source() {
let action = Error::action(
ErrorCode::TargetUnavailable,
receipt(crate::DispatchState::NotSent),
Error::element_not_found("#a"),
);
assert_eq!(action.receipt().unwrap().action, "click");
let cause = action.cause().unwrap();
assert_eq!(cause.code(), ErrorCode::ElementNotFound);
assert!(matches!(cause.detail(), Detail::Element { selector } if selector == "#a"));
assert_eq!(cause.message(), "Element not found: #a");
assert_eq!(action.message(), "Element not found: #a");
assert_eq!(
action.source().unwrap().to_string(),
"Element not found: #a"
);
let retry = Error::retry_exhausted(3, Error::timeout("slow"));
assert!(matches!(
retry.detail(),
Detail::RetryExhausted { attempts: 3, .. }
));
assert_eq!(retry.cause().unwrap().code(), ErrorCode::Timeout);
assert_eq!(retry.message(), "slow");
assert!(retry.receipt().is_none());
let cdp = Error::cdp("DOM.resolveNode", -32000, "gone");
assert_eq!(
cdp.cdp_context(),
Some((Some("DOM.resolveNode"), Some(-32000)))
);
assert_eq!(cdp.message(), "gone");
assert_eq!(Error::cdp_msg("x").cdp_context(), Some((None, None)));
assert!(Error::timeout("x").cdp_context().is_none());
let reply = Error::cdp_parts(None, Some(-32602), "bad").with_cdp_method("Page.navigate");
assert_eq!(
reply.cdp_context(),
Some((Some("Page.navigate"), Some(-32602)))
);
let bare = Error::cdp_msg("bad").with_cdp_method("Page.navigate");
assert_eq!(bare.cdp_context(), Some((None, None)));
let io_error = Error::transport_io("Failed to connect", io());
assert_eq!(io_error.source().unwrap().to_string(), "boom");
assert_eq!(io_error.clone().source().unwrap().to_string(), "boom");
assert!(Error::transport("plain").source().is_none());
assert!(Error::from(serde_json::from_str::<u8>("x").unwrap_err())
.source()
.is_some());
}