use super::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CdpFailure {
SessionGone,
ContextLost,
NodeGone,
NotRendered,
NotElement,
}
pub(crate) fn cdp_failure(error: &Error) -> Option<CdpFailure> {
error.cdp_context()?;
let message = error.message();
let any = |needles: &[&str]| needles.iter().any(|needle| message.contains(needle));
if any(&[
"Session with given id not found",
"No target with given id found",
"Not attached to an active page",
"Inspected target navigated or closed",
"Target closed",
]) {
Some(CdpFailure::SessionGone)
} else if any(&[
"Cannot find context",
"Execution context was destroyed",
"Could not find object",
"Cannot find object",
"Node with given id does not belong to the document",
]) {
Some(CdpFailure::ContextLost)
} else if any(&["Could not find node", "No node with given id"]) {
Some(CdpFailure::NodeGone)
} else if message.contains("box model") {
Some(CdpFailure::NotRendered)
} else if message.contains("Node is not an element") {
Some(CdpFailure::NotElement)
} else {
None
}
}
pub(crate) fn is(error: &Error, failure: CdpFailure) -> bool {
cdp_failure(error) == Some(failure)
}
pub(crate) fn element_unavailable(error: &Error) -> bool {
matches!(
error.code(),
super::ErrorCode::ElementNotFound | super::ErrorCode::ElementNotVisible
) || matches!(
cdp_failure(error),
Some(CdpFailure::NodeGone | CdpFailure::NotRendered | CdpFailure::NotElement)
)
}
fn cdp_message_contains(error: &Error, needles: &[&str]) -> bool {
error.cdp_context().is_some()
&& needles
.iter()
.any(|needle| error.message().contains(needle))
}
pub(crate) fn frame_discovery_raced(error: &Error) -> bool {
cdp_message_contains(
error,
&[
"No target with given id found",
"Session with given id not found",
"Not attached to an active page",
"No frame with given id found",
],
)
}
pub(crate) fn is_navigation_race(error: &Error) -> bool {
cdp_message_contains(
error,
&[
"Cannot find context with specified id",
"Execution context was destroyed",
"Inspected target navigated or closed",
],
)
}
pub(crate) fn is_stale_world(error: &Error) -> bool {
cdp_message_contains(
error,
&[
"Cannot find context with specified id",
"Execution context was destroyed",
],
)
}
pub(crate) fn world_replaced() -> Error {
Error::cdp(
"Runtime.evaluate",
-32000,
"Execution context was destroyed.",
)
}
pub(crate) fn node_left_document(error: &Error) -> bool {
cdp_message_contains(
error,
&["Node with given id does not belong to the document"],
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RecordingStartFailure {
Unsupported,
AlreadyActive,
Precondition,
}
pub(crate) fn recording_start_failure(error: &Error) -> Option<RecordingStartFailure> {
let (_, code) = error.cdp_context()?;
let text = error.message().to_ascii_lowercase();
if code == Some(-32601) || text.contains("not supported") || text.contains("wasn't found") {
return Some(RecordingStartFailure::Unsupported);
}
if text.contains("already active") {
return Some(RecordingStartFailure::AlreadyActive);
}
if text.contains("inactive")
|| text.contains("top-level")
|| text.contains("top target")
|| text.contains("not attached")
{
return Some(RecordingStartFailure::Precondition);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
fn is_node_gone(error: &Error) -> bool {
is(error, CdpFailure::NodeGone)
}
#[test]
fn classifies_known_chrome_messages() {
let cases = [
("Session with given id not found.", CdpFailure::SessionGone),
("No target with given id found", CdpFailure::SessionGone),
("Execution context was destroyed.", CdpFailure::ContextLost),
(
"Cannot find context with specified id",
CdpFailure::ContextLost,
),
("Could not find node with given id", CdpFailure::NodeGone),
("No node with given id found", CdpFailure::NodeGone),
("Could not compute box model.", CdpFailure::NotRendered),
("Node is not an element", CdpFailure::NotElement),
];
for (message, expected) in cases {
assert_eq!(
cdp_failure(&Error::cdp("X.y", -32000, message)),
Some(expected),
"{message}"
);
}
assert_eq!(
cdp_failure(&Error::cdp("X.y", -32000, "Invalid selector")),
None
);
assert_eq!(
cdp_failure(&Error::timeout("Execution context was destroyed")),
None
);
}
#[test]
fn only_known_discovery_races_are_retried() {
for message in [
"No target with given id found",
"Session with given id not found",
"Not attached to an active page",
"No frame with given id found",
] {
assert!(frame_discovery_raced(&Error::cdp_msg(message)), "{message}");
}
for message in [
"Invalid parameters",
"Target closed",
"Inspected target navigated or closed",
] {
assert!(
!frame_discovery_raced(&Error::cdp_msg(message)),
"{message}"
);
}
assert!(!frame_discovery_raced(&Error::decode(
"No target with given id found"
)));
}
#[test]
fn node_left_document_excludes_lost_objects() {
assert!(node_left_document(&Error::cdp(
"DOM.resolveNode",
-32000,
"Node with given id does not belong to the document"
)));
assert!(!node_left_document(&Error::cdp_msg("Cannot find object")));
assert!(!node_left_document(&Error::cdp_msg(
"Could not find object"
)));
assert!(!node_left_document(&Error::decode(
"Node with given id does not belong to the document"
)));
}
#[test]
fn only_a_lost_context_is_a_stale_world() {
for message in [
"Cannot find context with specified id",
"Execution context was destroyed.",
] {
assert!(
is_stale_world(&Error::cdp("Runtime.evaluate", -32000, message)),
"{message}"
);
}
assert!(is_stale_world(&world_replaced()));
for message in [
"Node with given id does not belong to the document",
"Could not find object with given id",
"Cannot find object with given id",
"Inspected target navigated or closed",
] {
assert!(
!is_stale_world(&Error::cdp("DOM.resolveNode", -32000, message)),
"{message}"
);
}
assert!(!is_stale_world(&Error::timeout(
"Execution context was destroyed"
)));
}
#[test]
fn classifies_recording_start_failures() {
let cases = [
(
-32601,
"'Page.startScreenRecording' wasn't found",
Some(RecordingStartFailure::Unsupported),
),
(
-32000,
"Screen recording is not supported",
Some(RecordingStartFailure::Unsupported),
),
(
-32000,
"Recording is already active",
Some(RecordingStartFailure::AlreadyActive),
),
(
-32000,
"Target is inactive",
Some(RecordingStartFailure::Precondition),
),
(
-32000,
"Only top-level targets can record",
Some(RecordingStartFailure::Precondition),
),
(
-32000,
"Not attached to an active page",
Some(RecordingStartFailure::Precondition),
),
(-32000, "Something else", None),
];
for (code, message, expected) in cases {
assert_eq!(
recording_start_failure(&Error::cdp("Page.startScreenRecording", code, message)),
expected,
"{message}"
);
}
assert_eq!(
recording_start_failure(&Error::timeout("not supported")),
None
);
}
#[test]
fn test_stale_node_error_detection() {
let stale = Error::cdp(
"DOM.resolveNode",
-32000,
"Could not find node with given id",
);
assert!(is_node_gone(&stale));
assert!(element_unavailable(&stale));
}
#[test]
fn test_box_model_error_is_element_but_not_stale() {
let box_model = Error::cdp("DOM.getBoxModel", -32000, "box model could not be computed");
assert!(element_unavailable(&box_model));
assert!(!is_node_gone(&box_model));
}
#[test]
fn test_element_not_found_is_element_but_not_stale() {
let not_found = Error::element_not_found("x");
assert!(element_unavailable(¬_found));
assert!(!is_node_gone(¬_found));
}
#[test]
fn test_navigation_race_detection() {
for message in [
"Cannot find context with specified id",
"Execution context was destroyed.",
"Inspected target navigated or closed",
] {
assert!(is_navigation_race(&Error::cdp(
"Runtime.evaluate",
-32000,
message
)));
}
let stale = Error::cdp(
"DOM.resolveNode",
-32000,
"Could not find node with given id",
);
assert!(!is_navigation_race(&stale));
assert!(!is_navigation_race(&Error::element_not_found("x")));
assert!(!is_navigation_race(&Error::transport("disconnected")));
}
#[test]
fn test_missing_node_message_variants() {
let stale = |message: &str| is_node_gone(&Error::cdp("DOM.x", -32000, message));
assert!(stale("Could not find node with given id"));
assert!(stale("No node with given id found"));
assert!(!stale("box model could not be computed"));
}
}