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> {
let Error::Cdp { message, .. } = error else {
return None;
};
let message = message.as_str();
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)
}
fn cdp_message_contains(error: &Error, needles: &[&str]) -> bool {
matches!(error, Error::Cdp { message, .. } if needles.iter().any(|needle| 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 node_left_document(error: &Error) -> bool {
cdp_message_contains(
error,
&["Node with given id does not belong to the document"],
)
}
#[cfg(test)]
mod tests {
use super::*;
#[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"
)));
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RecordingStartFailure {
Unsupported,
AlreadyActive,
Precondition,
}
pub(crate) fn recording_start_failure(error: &Error) -> Option<RecordingStartFailure> {
let Error::Cdp { code, message, .. } = error else {
return None;
};
let text = 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 recording_tests {
use super::*;
#[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
);
}
}