The headless browser for AI agents and web scraping
fix(cdp): report a thrown or rejected value through exceptionDetails
Nguyen Thanh Datef693edbac4c
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rsindex eeecddb8..5fdbdd0f 100644--- a/crates/obscura-browser/src/page.rs+++ b/crates/obscura-browser/src/page.rs@@ -3650,6 +3650,7 @@ impl Page {Err(e) => {tracing::debug!("evaluate_for_cdp error: {}", e);obscura_js::runtime::RemoteObjectInfo {+ thrown: false,js_type: "undefined".into(),subtype: None,class_name: String::new(),@@ -3662,6 +3663,7 @@ impl Page {} else {let val = self.evaluate(expression);obscura_js::runtime::RemoteObjectInfo {+ thrown: false,js_type: match &val {serde_json::Value::String(_) => "string".into(),serde_json::Value::Number(_) => "number".into(),@@ -3695,6 +3697,7 @@ impl Page {} else {let value = self.evaluate(expression);Ok(obscura_js::runtime::RemoteObjectInfo {+ thrown: false,js_type: match &value {serde_json::Value::String(_) => "string".into(),serde_json::Value::Number(_) => "number".into(),@@ -3733,6 +3736,7 @@ impl Page {Err(e) => {tracing::debug!("callFunctionOn error: {}", e);obscura_js::runtime::RemoteObjectInfo {+ thrown: false,js_type: "undefined".into(),subtype: None,class_name: String::new(),@@ -3744,6 +3748,7 @@ impl Page {}} else {obscura_js::runtime::RemoteObjectInfo {+ thrown: false,js_type: "undefined".into(),subtype: None,class_name: String::new(),diff --git a/crates/obscura-cdp/src/domains/runtime.rs b/crates/obscura-cdp/src/domains/runtime.rsindex 6edd68e3..44817a24 100644--- a/crates/obscura-cdp/src/domains/runtime.rs+++ b/crates/obscura-cdp/src/domains/runtime.rs@@ -149,7 +149,7 @@ pub async fn handle(};emit_post_eval_nav(ctx, session_id).await?;- Ok(json!({ "result": remote_object_from_info(&info) }))+ Ok(evaluation_reply(&info))}"callFunctionOn" => {let function_declaration = params@@ -213,7 +213,7 @@ pub async fn handle(};emit_post_eval_nav(ctx, session_id).await?;- Ok(json!({ "result": remote_object_from_info(&info) }))+ Ok(evaluation_reply(&info))}"getProperties" => {// Puppeteer's $$() flow:@@ -455,6 +455,33 @@ fn validate_context_id(Ok(())}+/// Shape one `Runtime.evaluate` or `Runtime.callFunctionOn` reply.+///+/// A thrown value, or the value a promise rejected with, is not a protocol+/// failure: the command succeeds and reports it through `exceptionDetails`,+/// which is what a client rebuilds the page error from. The value is repeated+/// in `result` the way Chrome does, so a client that reads only `result` sees+/// the error object rather than a success that never happened.+///+/// `exceptionId` is a fixed 1 because nothing here correlates exceptions+/// across commands; clients key off the presence of the field, not its id.+fn evaluation_reply(info: &RemoteObjectInfo) -> Value {+ let remote = remote_object_from_info(info);+ if !info.thrown {+ return json!({ "result": remote });+ }+ json!({+ "result": remote.clone(),+ "exceptionDetails": {+ "exceptionId": 1,+ "text": "Uncaught",+ "lineNumber": 0,+ "columnNumber": 0,+ "exception": remote,+ }+ })+}+fn remote_object_from_info(info: &RemoteObjectInfo) -> Value {let mut obj = json!({ "type": info.js_type });@@ -557,6 +584,99 @@ mod tests {}}+ #[tokio::test(flavor = "current_thread")]+ async fn evaluate_reports_a_rejection_through_exception_details() {+ let mut ctx = CdpContext::new();+ let page_id = ctx.create_page();+ let session_id = "evaluate-rejection".to_string();+ ctx.sessions.insert(session_id.clone(), page_id);++ let reply = handle(+ "evaluate",+ &json!({+ "expression": "Promise.reject(new Error('boom'))",+ "returnByValue": true,+ "awaitPromise": true,+ "timeout": 3000,+ }),+ &mut ctx,+ &Some(session_id),+ )+ .await+ .expect("a rejection is answered, not failed at the protocol level");++ let details = reply+ .get("exceptionDetails")+ .expect("a thrown value is reported through exceptionDetails");+ assert_eq!(details["text"], json!("Uncaught"));+ assert_eq!(details["exception"]["subtype"], json!("error"));+ assert_eq!(details["exception"]["className"], json!("Error"));+ assert!(+ details["exception"]["description"]+ .as_str()+ .unwrap_or_default()+ .contains("boom"),+ "the description carries the page message: {details}"+ );+ // Chrome repeats the exception in `result`, so a client that reads+ // only that field still sees the error rather than a stale success.+ assert_eq!(reply["result"], details["exception"]);+ }++ #[tokio::test(flavor = "current_thread")]+ async fn call_function_on_reports_a_rejection_through_exception_details() {+ // This is the path Puppeteer's page.evaluate(fn) takes. It used to+ // answer successfully with `{}` for a rejected Error, so the caller+ // could not tell a failure from an empty object.+ let mut ctx = CdpContext::new();+ let page_id = ctx.create_page();+ let session_id = "call-rejection".to_string();+ ctx.sessions.insert(session_id.clone(), page_id);++ let reply = handle(+ "callFunctionOn",+ &json!({+ "functionDeclaration": "() => Promise.reject(new Error('boom'))",+ "returnByValue": true,+ "awaitPromise": true,+ "timeout": 3000,+ }),+ &mut ctx,+ &Some(session_id),+ )+ .await+ .expect("a rejection is answered, not failed at the protocol level");++ let details = reply+ .get("exceptionDetails")+ .expect("a rejected call is reported through exceptionDetails");+ assert_eq!(details["exception"]["subtype"], json!("error"));+ assert!(reply["result"]["value"].is_null(), "the error must not be serialized by value: {reply}");+ }++ #[tokio::test(flavor = "current_thread")]+ async fn a_resolved_evaluation_carries_no_exception_details() {+ let mut ctx = CdpContext::new();+ let page_id = ctx.create_page();+ let session_id = "evaluate-resolved".to_string();+ ctx.sessions.insert(session_id.clone(), page_id);++ let reply = handle(+ "evaluate",+ &json!({+ "expression": "Promise.resolve(2)",+ "returnByValue": true,+ "awaitPromise": true,+ "timeout": 3000,+ }),+ &mut ctx,+ &Some(session_id),+ )+ .await+ .unwrap();+ assert_eq!(reply["result"]["value"], json!(2.0));+ assert!(reply.get("exceptionDetails").is_none());+ }#[tokio::test(flavor = "current_thread")]async fn evaluate_await_promise_reports_the_requested_timeout() {let mut ctx = CdpContext::new();diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 7114a5bd..f31e8695 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -107,6 +107,11 @@ fn with_sync_render_loading_disabled<R>(#[derive(Debug, Clone)]pub struct RemoteObjectInfo {+ /// True when this object is a value that was thrown or that a promise+ /// rejected with, rather than the evaluation result. CDP reports those+ /// through `exceptionDetails` on a successful reply, so the difference+ /// has to survive the trip out of the runtime.+ pub thrown: bool,pub js_type: String,pub subtype: Option<String>,pub class_name: String,@@ -1605,12 +1610,14 @@ impl ObscuraJsRuntime {).map_err(|e| format!("JS error: {}", e))?;if self.v8_to_json(rejected)?.as_bool().unwrap_or(false) {- let err = self.execute_runtime_script("<readError>", format!("String(globalThis.__obscura_objects['{0}'] && (globalThis.__obscura_objects['{0}'].message || globalThis.__obscura_objects['{0}']))", oid))- .map_err(|e| format!("JS error: {}", e))?;- return Err(format!(- "Promise rejected: {}",- self.v8_to_json(err)?.as_str().unwrap_or("")- ));+ // A rejection is not a protocol failure. CDP answers the+ // command and puts the rejected value in `exceptionDetails`,+ // which is what a client rebuilds the page error from, so the+ // value travels back as a remote object flagged `thrown`.+ // It is reported by reference even when the caller asked for a+ // value: `JSON.stringify(new Error("boom"))` is `{}`, so+ // serializing it would throw the message away.+ return self.thrown_info(&oid);}self.execute_runtime_script("<readMeta>", "globalThis.__obscura_await_meta".to_string()).map_err(|e| format!("JS error: {}", e))?@@ -1690,10 +1697,12 @@ impl ObscuraJsRuntime {__result = await __fn.call(__this, {args});\n\globalThis.__obscura_objects['{oid}'] = __result;\n\globalThis.__obscura_await_meta = {meta_fn};\n\+ globalThis.__obscura_await_rejected = false;\n\}} catch(e) {{\n\__result = e;\n\globalThis.__obscura_objects['{oid}'] = e;\n\globalThis.__obscura_await_meta = {err_meta_fn};\n\+ globalThis.__obscura_await_rejected = true;\n\}} finally {{\n\globalThis.__obscura_done_{done_counter} = true;\n\}}\n\@@ -1744,6 +1753,22 @@ impl ObscuraJsRuntime {);}+ // Same rule as evaluate: a rejected call is answered, not failed,+ // and the value is never serialized by value. Without this the+ // wrapper stored the error under the object id a success uses, so+ // a rejection came back as an ordinary result: an Error as `{}`,+ // and `Promise.reject({code: 42})` as `{code: 42}`, which is+ // indistinguishable from resolving with it.+ let rejected = self+ .execute_runtime_script(+ "<readRejected>",+ "globalThis.__obscura_await_rejected".to_string(),+ )+ .map_err(|e| format!("JS error: {}", e))?;+ if self.v8_to_json(rejected)?.as_bool().unwrap_or(false) {+ return self.thrown_info(&oid);+ }+if return_by_value {let read = self.execute_runtime_script(@@ -2935,6 +2960,12 @@ impl ObscuraJsRuntime {cn = 'Function';desc = v.name ? 'function ' + v.name + '()' : 'function()';}}+ else if (t === 'object' && v instanceof Error) {{+ st = 'error';+ cn = (v.constructor && v.constructor.name) || 'Error';+ desc = (typeof v.stack === 'string' && v.stack) ? v.stack+ : (cn + (v.message ? ': ' + v.message : ''));+ }}else if (t === 'object') {{cn = (v.constructor && v.constructor.name) || 'Object';desc = cn;@@ -3047,6 +3078,7 @@ impl ObscuraJsRuntime {fn info_from_json(value: &serde_json::Value) -> RemoteObjectInfo {match value {serde_json::Value::Null => RemoteObjectInfo {+ thrown: false,js_type: "object".into(),subtype: Some("null".into()),class_name: String::new(),@@ -3055,6 +3087,7 @@ impl ObscuraJsRuntime {value: Some(serde_json::Value::Null),},serde_json::Value::Bool(b) => RemoteObjectInfo {+ thrown: false,js_type: "boolean".into(),subtype: None,class_name: String::new(),@@ -3063,6 +3096,7 @@ impl ObscuraJsRuntime {value: Some(value.clone()),},serde_json::Value::Number(n) => RemoteObjectInfo {+ thrown: false,js_type: "number".into(),subtype: None,class_name: String::new(),@@ -3071,6 +3105,7 @@ impl ObscuraJsRuntime {value: Some(value.clone()),},serde_json::Value::String(s) => RemoteObjectInfo {+ thrown: false,js_type: "string".into(),subtype: None,class_name: String::new(),@@ -3079,6 +3114,7 @@ impl ObscuraJsRuntime {value: Some(value.clone()),},serde_json::Value::Array(arr) => RemoteObjectInfo {+ thrown: false,js_type: "object".into(),subtype: Some("array".into()),class_name: "Array".into(),@@ -3087,6 +3123,7 @@ impl ObscuraJsRuntime {value: Some(value.clone()),},serde_json::Value::Object(_) => RemoteObjectInfo {+ thrown: false,js_type: "object".into(),subtype: None,class_name: "Object".into(),@@ -3097,6 +3134,33 @@ impl ObscuraJsRuntime {}}+ /// Build the remote object for a value that was thrown, or that a promise+ /// rejected with.+ ///+ /// Both wrappers have already stored the value under `oid` and put its+ /// metadata in `__obscura_await_meta`, so this reads them back and marks+ /// the result. The mark is what lets the CDP layer answer the command with+ /// `exceptionDetails` rather than fail it or, worse, present the value as+ /// the evaluation result.+ fn thrown_info(&mut self, oid: &str) -> Result<RemoteObjectInfo, String> {+ let meta = self+ .execute_runtime_script("<readMeta>", "globalThis.__obscura_await_meta".to_string())+ .map_err(|e| format!("JS error: {}", e))?;+ let meta = self.v8_to_json(meta)?;+ let meta_json = if let serde_json::Value::String(text) = &meta {+ serde_json::from_str(text).unwrap_or_else(|_| meta.clone())+ } else {+ meta+ };+ self.object_store.insert(+ oid.to_string(),+ format!("globalThis.__obscura_objects['{}']", oid),+ );+ let mut info = Self::info_from_meta(&meta_json, Some(oid.to_string()));+ info.thrown = true;+ Ok(info)+ }+fn info_from_meta(meta: &serde_json::Value, object_id: Option<String>) -> RemoteObjectInfo {let js_type = meta.get("type")@@ -3127,6 +3191,7 @@ impl ObscuraJsRuntime {};RemoteObjectInfo {+ thrown: false,js_type,subtype,class_name,@@ -13665,14 +13730,90 @@ mod tests {assert_eq!(result.value.unwrap().as_str().unwrap(), "async-ok");}+ // This test used to assert that a rejection came back as `Err`. It does+ // not any more, and the old expectation was the defect: CDP answers the+ // command and reports the rejected value through `exceptionDetails`, so+ // failing the command loses the page error rather than delivering it.#[tokio::test(flavor = "current_thread")]async fn test_evaluate_for_cdp_reports_promise_rejection() {let mut rt = setup_runtime("<html><body></body></html>");- let err = rt+ let info = rt.evaluate_for_cdp("Promise.reject(new Error('boom'))", true, true).await- .unwrap_err();- assert!(err.contains("boom"));+ .expect("a rejection is answered, not failed");+ assert!(info.thrown, "the rejected value must be marked as thrown");+ assert_eq!(info.js_type, "object");+ assert_eq!(info.subtype.as_deref(), Some("error"));+ assert_eq!(info.class_name, "Error");+ assert!(+ info.description.contains("boom"),+ "the description is what a client rebuilds the error from: {}",+ info.description+ );+ // Reported by reference, never serialized: an Error has no own+ // enumerable properties, so by value it would be `{}`.+ assert!(info.value.is_none());+ assert!(info.object_id.is_some());+ }++ #[tokio::test(flavor = "current_thread")]+ async fn call_function_on_marks_a_rejection_instead_of_returning_it() {+ // The reported shape: returnByValue on a rejected call answered+ // successfully with `{}`, because the wrapper stored the error under+ // the object id a success uses and nothing said which branch ran.+ let mut rt = setup_runtime("<html><body></body></html>");+ let info = rt+ .call_function_on_for_cdp("() => Promise.reject(new Error('boom'))", None, &[], true, true)+ .await+ .expect("a rejection is answered, not failed");+ assert!(info.thrown, "expected a thrown value, got {:?}", info.value);+ assert_eq!(info.subtype.as_deref(), Some("error"));+ assert!(info.description.contains("boom"));+ }++ #[tokio::test(flavor = "current_thread")]+ async fn call_function_on_separates_a_rejected_object_from_a_resolved_one() {+ // The sharper half of the same defect: rejecting with a plain object+ // produced a reply byte-identical to resolving with it, so no client+ // could tell the two apart.+ let mut rt = setup_runtime("<html><body></body></html>");+ let resolved = rt+ .call_function_on_for_cdp("() => Promise.resolve({code: 42})", None, &[], true, true)+ .await+ .unwrap();+ let rejected = rt+ .call_function_on_for_cdp("() => Promise.reject({code: 42})", None, &[], true, true)+ .await+ .unwrap();+ assert!(!resolved.thrown);+ assert!(rejected.thrown);+ assert_eq!(resolved.value, Some(serde_json::json!({"code": 42})));+ }++ #[tokio::test(flavor = "current_thread")]+ async fn a_rejection_does_not_leak_into_the_next_call() {+ // `__obscura_await_rejected` is a global, so the success branch has to+ // clear it. Without that the first rejection would mark every later+ // call on the same runtime as thrown.+ let mut rt = setup_runtime("<html><body></body></html>");+ let rejected = rt+ .call_function_on_for_cdp("() => Promise.reject(new Error('first'))", None, &[], true, true)+ .await+ .unwrap();+ assert!(rejected.thrown);+ let after = rt+ .call_function_on_for_cdp("() => Promise.resolve(7)", None, &[], true, true)+ .await+ .unwrap();+ assert!(!after.thrown, "a later success was still marked as thrown");+ assert_eq!(after.value.unwrap().as_f64(), Some(7.0));++ let evaluated = rt+ .evaluate_for_cdp("Promise.resolve(8)", true, true)+ .await+ .unwrap();+ assert!(!evaluated.thrown);+ assert_eq!(evaluated.value.unwrap().as_f64(), Some(8.0));}#[tokio::test(flavor = "current_thread")]