Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(cdp): commit error documents for offline Fetch continuations
ldm0846b24119a48
diff --git a/moli-protocol/src/conn/runtime_load.rs b/moli-protocol/src/conn/runtime_load.rsindex fd0a67fb8..16189d76a 100644--- a/moli-protocol/src/conn/runtime_load.rs+++ b/moli-protocol/src/conn/runtime_load.rs@@ -2300,6 +2300,36 @@ impl CdpConnection {.await}+ pub(crate) async fn prepare_navigation_load_error_for_navigation_async(+ &mut self,+ navigation: &NavigationDispatchState,+ error_text: String,+ ) -> Result<NavigationLoadOutcome, String> {+ // Fetch continuation loaders currently share a String error boundary+ // for protocol errors and network failures. Offline emulation is a+ // navigation failure that commits an error Document, just as it does+ // in BackgroundNavigationLoadJob; retain other errors unchanged.+ if error_text != NET_ERR_INTERNET_DISCONNECTED_ERROR_TEXT {+ return Err(error_text);+ }++ let load_inputs = self.navigation_load_inputs_for_navigation(navigation);+ let mut engine = self.navigation_engine_handle_for_load_inputs(&load_inputs);+ let page_reservation =+ self.reserve_renderer_page_for_owner(&navigation.owner, &load_inputs, &engine);+ prepare_network_error_page_navigation_with_engine_async(+ &mut engine,+ page_reservation,+ &load_inputs,+ navigation.requested_url.clone(),+ navigation.request_method.clone(),+ navigation.request_headers.clone(),+ error_text,+ RendererReplyBoundary::Stage,+ )+ .await+ }+#[allow(clippy::too_many_arguments)]async fn load_navigation_request_via_runtime_with_network_events_and_load_inputs_async(&mut self,diff --git a/moli-protocol/src/domains/fetch/navigation.rs b/moli-protocol/src/domains/fetch/navigation.rsindex ea4598892..92be15a5a 100644--- a/moli-protocol/src/domains/fetch/navigation.rs+++ b/moli-protocol/src/domains/fetch/navigation.rs@@ -437,6 +437,17 @@ async fn complete_pending_fetch_navigation_result_into_buffer_async() {let token = pending.document_navigation_token;let navigation_state = pending.navigation;+ let navigation =+ match navigation {+ Err(error_text) => {+ Box::pin(conn.prepare_navigation_load_error_for_navigation_async(+ &navigation_state,+ error_text,+ ))+ .await+ }+ navigation => navigation,+ };let navigation =network::materialize_navigation_load_result(conn, &navigation_state, navigation);complete_tokened_materialized_navigation_into_buffer_async(diff --git a/moli-protocol/src/domains/fetch/tests/navigation_control.rs b/moli-protocol/src/domains/fetch/tests/navigation_control.rsindex 9227b47d1..3d935a67a 100644--- a/moli-protocol/src/domains/fetch/tests/navigation_control.rs+++ b/moli-protocol/src/domains/fetch/tests/navigation_control.rs@@ -303,10 +303,20 @@ async fn fail_request_blocked_by_client_maps_main_document_navigation_to_net_err}#[tokio::test(flavor = "multi_thread")]-async fn request_paused_then_continue_request_fails_when_network_offline() {+async fn request_paused_then_continue_request_commits_offline_error_document() {+ assert_continued_offline_navigation_commits_error_document(false).await;+}++#[tokio::test(flavor = "multi_thread")]+async fn request_paused_then_continue_request_with_auth_handling_commits_offline_error_document() {+ assert_continued_offline_navigation_commits_error_document(true).await;+}++async fn assert_continued_offline_navigation_commits_error_document(handle_auth_requests: bool) {let mut ctx = TestContext::new();ctx.conn.install_browser_context_fixture_for_test(attached_browser_context());+ ctx.enable_page_events_for_test(Some("SID-1"));ctx.process_async(json!({"id": 16630,@@ -319,7 +329,8 @@ async fn request_paused_then_continue_request_fails_when_network_offline() {ctx.process_async(json!({"id": 16631,"method": "Fetch.enable",- "sessionId": "SID-1"+ "sessionId": "SID-1",+ "params": { "handleAuthRequests": handle_auth_requests }})).await;ctx.expect_result(16631, json!({}), Some("SID-1"));@@ -338,10 +349,16 @@ async fn request_paused_then_continue_request_fails_when_network_offline() {.as_str().expect("fetch request id").to_owned();+ let network_id = paused["params"]["networkId"].clone();+ assert!(+ network_id.is_string(),+ "paused document must have a Network request id"+ );ctx.process_async(json!({"id": 16633,"method": "Network.emulateNetworkConditions",+ "sessionId": "SID-1","params": {"offline": true,"latency": 0,@@ -350,7 +367,7 @@ async fn request_paused_then_continue_request_fails_when_network_offline() {}})).await;- ctx.expect_result(16633, json!({}), None);+ ctx.expect_result(16633, json!({}), Some("SID-1"));ctx.process_async(json!({"id": 16634,@@ -360,14 +377,78 @@ async fn request_paused_then_continue_request_fails_when_network_offline() {})).await;ctx.expect_result(16634, json!({}), Some("SID-1"));- let failed = ctx.take_one();- assert_eq!(failed["method"], "Network.loadingFailed");- assert_eq!(failed["sessionId"], "SID-1");++ wait_until_message(+ &mut ctx,+ Some("SID-1"),+ "continued offline navigation result",+ |message| message["id"] == json!(16632),+ )+ .await;+ let response = take_response_by_id(&mut ctx, 16632);+ assert!(+ response.get("error").is_none(),+ "offline navigation must return a navigation result, not a protocol error: {response}"+ );+ assert_eq!(response["sessionId"], "SID-1");+ assert_eq!(response["result"]["frameId"], "TID-1");+ assert_eq!(response["result"]["loaderId"], network_id);+ assert_eq!(response["result"]["isDownload"], false);+ assert_eq!(+ response["result"]["errorText"],+ "net::ERR_INTERNET_DISCONNECTED"+ );++ wait_until_frame_stopped_loading(&mut ctx, "TID-1").await;+ let messages = ctx.take_all();+ let failed = messages+ .iter()+ .find(|message| {+ message["method"] == json!("Network.loadingFailed")+ && message["sessionId"] == json!("SID-1")+ && message["params"]["requestId"] == network_id+ })+ .unwrap_or_else(|| panic!("missing document loadingFailed: {messages:?}"));+ assert_eq!(failed["params"]["type"], "Document");+ assert_eq!(failed["params"]["canceled"], false);assert_eq!(failed["params"]["errorText"],"net::ERR_INTERNET_DISCONNECTED");- ctx.expect_error(16632, -32000, "net::ERR_INTERNET_DISCONNECTED");+ let frame = messages+ .iter()+ .find(|message| {+ message["method"] == json!("Page.frameNavigated")+ && message["sessionId"] == json!("SID-1")+ })+ .unwrap_or_else(|| panic!("missing error document commit: {messages:?}"));+ assert_eq!(frame["params"]["frame"]["id"], "TID-1");+ assert_eq!(frame["params"]["frame"]["loaderId"], network_id);+ assert_eq!(frame["params"]["frame"]["url"], NETWORK_ERROR_PAGE_URL);+ assert_eq!(+ frame["params"]["frame"]["unreachableUrl"],+ "http://example.test/offline"+ );++ ctx.process_async(json!({+ "id": 16635,+ "method": "Runtime.evaluate",+ "sessionId": "SID-1",+ "params": {+ "expression": "({online: navigator.onLine, sum: 1 + 1})",+ "returnByValue": true+ }+ }))+ .await;+ let runtime = take_response_by_id(&mut ctx, 16635);+ assert!(+ runtime.get("error").is_none(),+ "error document must remain usable: {runtime}"+ );+ assert_eq!(+ runtime["result"]["result"]["value"],+ json!({"online": false, "sum": 2})+ );}#[tokio::test(flavor = "multi_thread")]