Skip to content

The headless browser for AI agents and web scraping

fix(js): console.log(Error) must not trigger Error.prepareStackTrace

SGavrl3c92c368627d
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 1ba9463c..d5e7a933 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -212,7 +212,13 @@ const _consoleFn = (level, args) => {
try { Deno.core.ops.op_console_msg(level, args.map(a => {
if (a === null) return "null";
if (a === undefined) return "undefined";
- if (a instanceof Error) return a.stack || a.message || String(a);
+ if (a instanceof Error) {
+ const _pst = Error.prepareStackTrace;
+ if (_pst !== undefined) Error.prepareStackTrace = undefined;
+ const _s = a.stack || a.message || String(a);
+ if (_pst !== undefined) Error.prepareStackTrace = _pst;
+ return _s;
+ }
if (typeof a === "object") {
try {
const s = JSON.stringify(a);
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 92908d79..9b0545a1 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -2452,6 +2452,21 @@ mod tests {
);
}
+ #[test]
+ fn console_log_error_does_not_trigger_prepare_stack_trace() {
+ let mut rt = setup_runtime("<div></div>");
+ let result = rt.evaluate(r#"
+ let called = false;
+ const saved = Error.prepareStackTrace;
+ Error.prepareStackTrace = function() { called = true; return saved; };
+ const e = new Error("test");
+ console.log(e);
+ Error.prepareStackTrace = saved;
+ return called;
+ "#).unwrap();
+ assert_eq!(result, serde_json::json!(false));
+ }
+
#[test]
fn element_aria_reflection_setters_write_through() {
let mut rt = setup_runtime(r#"<div id="d"></div>"#);