-
Notifications
You must be signed in to change notification settings - Fork 5.1k
webcore: preserve Error cause across structuredClone/postMessage/v8 serialization #31525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alii
wants to merge
8
commits into
main
Choose a base branch
from
ali/ssv-error-cause
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3dee773
webcore: preserve Error cause across structuredClone/postMessage/v8 s…
alii 852ba19
webcore: root the pending Error cause in m_gcBuffer while it awaits s…
robobun 881b097
test(workers): deflake the event-loop tests by awaiting subprocess exit
robobun 9936f4c
test: verify Error cause survives structuredClone/v8 identically in N…
alii df75e22
[autofix.ci] apply automated fixes
autofix-ci[bot] 90d254e
test: run the Error cause node:test parity file under node --test in CI
robobun 6d504cf
docs: add ErrorInstanceTag to the serialization grammar comment
robobun 37279fb
build: exclude the xxhash3 Highway TU from unified sources
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Uses node:test (not bun:test) so the exact same file runs under both | ||
| // `node --test` and `bun test` — proving structuredClone/v8 preserve an Error's | ||
| // `cause` identically in both runtimes. Byte-exact serialization is NOT a goal | ||
| // (Bun uses WebKit's SerializedScriptValue format, Node uses V8's serializer); | ||
| // what must match is the observable result of cloning/round-tripping. | ||
| // | ||
| // structured-clone.test.ts spawns `node --test` on this file so the Node.js | ||
| // side of the parity claim is enforced in CI, not just runnable by hand. | ||
| import assert from "node:assert/strict"; | ||
| import { test } from "node:test"; | ||
| import v8 from "node:v8"; | ||
|
|
||
| const clones: Array<[string, (value: any) => any]> = [ | ||
| ["structuredClone", structuredClone], | ||
| ["v8 round-trip", value => v8.deserialize(v8.serialize(value))], | ||
| ]; | ||
|
|
||
| for (const [name, clone] of clones) { | ||
| test(`${name} preserves a string cause as a non-enumerable own data property`, () => { | ||
| const e = clone(new TypeError("typed", { cause: "boom" })); | ||
| assert.ok(e instanceof TypeError); | ||
| assert.equal(e.message, "typed"); | ||
| assert.deepEqual(Object.getOwnPropertyDescriptor(e, "cause"), { | ||
| value: "boom", | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
|
|
||
| test(`${name} leaves an Error without a cause unchanged`, () => { | ||
| const e = clone(new Error("nc")); | ||
| assert.ok(!Object.hasOwn(e, "cause")); | ||
| assert.equal(e.cause, undefined); | ||
| }); | ||
|
|
||
| test(`${name} preserves a number cause`, () => { | ||
| assert.equal(clone(new Error("x", { cause: 42 })).cause, 42); | ||
| }); | ||
|
|
||
| test(`${name} preserves an explicit undefined cause as an own property`, () => { | ||
| const e = clone(new Error("x", { cause: undefined })); | ||
| assert.ok(Object.hasOwn(e, "cause")); | ||
| assert.equal(e.cause, undefined); | ||
| }); | ||
|
|
||
| test(`${name} preserves an object cause structurally`, () => { | ||
| const e = clone(new Error("x", { cause: { code: 42, nested: { ok: true } } })); | ||
| assert.deepEqual(e.cause, { code: 42, nested: { ok: true } }); | ||
| }); | ||
|
|
||
| test(`${name} preserves a nested Error cause as an Error of the right type`, () => { | ||
| const e = clone(new Error("outer", { cause: new RangeError("inner") })); | ||
| assert.ok(e.cause instanceof RangeError); | ||
| assert.equal(e.cause.message, "inner"); | ||
| assert.ok(!Object.hasOwn(e.cause, "cause")); | ||
| }); | ||
|
|
||
| test(`${name} preserves a chain of Error causes`, () => { | ||
| const a = new Error("a"); | ||
| const b = new Error("b", { cause: a }); | ||
| const e = clone(new Error("c", { cause: b })); | ||
| assert.equal(e.message, "c"); | ||
| assert.equal(e.cause.message, "b"); | ||
| assert.equal(e.cause.cause.message, "a"); | ||
| assert.ok(!Object.hasOwn(e.cause.cause, "cause")); | ||
| }); | ||
|
|
||
| test(`${name} preserves a cyclic cause as the same reference, non-enumerable`, () => { | ||
| const o = new Error("self"); | ||
| o.cause = o; // an enumerable own property on the input | ||
| const e = clone(o); | ||
| assert.equal(e.cause, e); | ||
| assert.equal(Object.getOwnPropertyDescriptor(e, "cause")!.enumerable, false); | ||
| }); | ||
|
|
||
| test(`${name} keeps identity when the same Error appears twice`, () => { | ||
| const o = new Error("shared", { cause: "x" }); | ||
| const e = clone([o, o]); | ||
| assert.equal(e[0], e[1]); | ||
| assert.equal(e[0].cause, "x"); | ||
| }); | ||
|
|
||
| test(`${name} preserves cause and identity inside containers`, () => { | ||
| const o = new Error("e", { cause: [1, 2, 3] }); | ||
| const e = clone({ arr: [o], map: new Map([["k", o]]) }); | ||
| assert.ok(e.arr[0] instanceof Error); | ||
| assert.deepEqual(e.arr[0].cause, [1, 2, 3]); | ||
| assert.equal(e.map.get("k"), e.arr[0]); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.