From 9cd04d0a8815ee4df5fde1cdbda84e6d7d07363d Mon Sep 17 00:00:00 2001 From: hugocasa Date: Thu, 5 Nov 2026 23:29:19 +0100 Subject: [PATCH] fix: keep the real error when a Bun script throws a cyclic error (#12232) * fix: keep the real error when a Bun script throws a cyclic error The Bun wrapper copies a thrown error's own properties into `extra` and JSON-encodes the result. When one of them is cyclic (AWS SDK v3 errors' `$response`), the encoding threw and the job error became "cannot serialize cyclic structures". Fall back to an encoding that replaces back-references with "[Circular]", keeping the plain encoding first so non-cyclic errors are unchanged. Co-Authored-By: Claude Opus 5.5 * fix: replace only the unencodable fields of a Bun error's extra Encode each field of `extra` on its own when the whole error fails, and replace the ones that throw with "[unserializable]". Simpler than walking cycles, and drops noise like AWS's `$response` while keeping `Code` and `$metadata`. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- backend/tests/bun_jobs.rs | 7 ++++++- backend/windmill-worker/src/bun_executor.rs | 21 +++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/backend/tests/bun_jobs.rs b/backend/tests/bun_jobs.rs index 50fad28a04..071842ae16 100644 --- a/backend/tests/bun_jobs.rs +++ b/backend/tests/bun_jobs.rs @@ -330,7 +330,10 @@ async fn test_bun_job_runtime_error(db: Pool) -> anyhow::Result<()> { let content = r#" export function main() { - throw new Error("intentional error"); + const e = new Error("intentional error"); + e.Code = "X"; + e.self = e; + throw e; } "# .to_owned(); @@ -361,6 +364,8 @@ export function main() { .as_str() .unwrap() .contains("intentional error")); + assert_eq!(error["extra"]["Code"], "X"); + assert_eq!(error["extra"]["self"], "[unserializable]"); Ok(()) } diff --git a/backend/windmill-worker/src/bun_executor.rs b/backend/windmill-worker/src/bun_executor.rs index c1e1d95616..545ebbb717 100644 --- a/backend/windmill-worker/src/bun_executor.rs +++ b/backend/windmill-worker/src/bun_executor.rs @@ -85,6 +85,23 @@ pub const BUN_LOCKB_SPLIT_WINDOWS: &str = "\r\n//bun.lockb\r\n"; pub const EMPTY_FILE: &str = ""; +/// A thrown error's own properties (`extra`) can be cyclic, e.g. AWS SDK v3's `$response`: +/// a plain `JSON.stringify` then throws and the real error is lost. The fallback replaces +/// only the fields that cannot be encoded, so the rest (`Code`, `$metadata`, ...) survive. +const STRINGIFY_ERROR_JS: &str = r#"(() => { + try { + return JSON.stringify(err); + } catch {} + for (const k of Object.keys(err.extra ?? {})) { + try { + JSON.stringify(err.extra[k]); + } catch { + err.extra[k] = '[unserializable]'; + } + } + return JSON.stringify(err); + })()"#; + /// Bun args for dedicated worker (without the script path) pub const BUN_DEDICATED_WORKER_ARGS: &[&str] = &["run", "-i", "--prefer-offline"]; @@ -2870,7 +2887,7 @@ try {{ if (Object.keys(extra).length > 0) {{ err["extra"] = extra; }} - await fs.writeFile("result.json", JSON.stringify(err)); + await fs.writeFile("result.json", {STRINGIFY_ERROR_JS}); process.exit(1); }} "#, @@ -2944,7 +2961,7 @@ try {{ if (Object.keys(extra).length > 0) {{ err["extra"] = extra; }} - await fs.writeFile("result.json", JSON.stringify(err)); + await fs.writeFile("result.json", {STRINGIFY_ERROR_JS}); process.exit(1); }} "#,