Repository navigation
node errors: put an own enumerable code on ERR_* errors in one ErrorCodeCache factory #41737
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
robobun
wants to merge
13
commits into
main
Choose a base branch
from
robobun/f4e8da95/enumerable-error-code
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 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a58615f
Make the code property of NodeError instances enumerable
robobun 3e90537
Mint the own code from the table literal and leave Error warnings unt…
robobun ba647b4
test: use a concrete deprecation code in the emitWarning passthrough …
robobun 83a4fe6
Shorten comments
robobun dd20209
Keep the code in NodeError toString independent of this.code
robobun 06af0af
Shorten the errorCodeIs comment
robobun bfd2ee6
Merge branch 'main' into robobun/f4e8da95/enumerable-error-code
robobun d2b6125
node errors: put an own enumerable code on ERR_* errors in one ErrorC…
robobun c72185d
Merge branch 'main' into robobun/f4e8da95/enumerable-error-code
robobun 6c08701
child_process: link the line of Node that stamps the syscall
robobun 947886b
node errors: keep the name of the kind on the per-code prototype
robobun 8438a18
Move the tests of what spawn() throws to child_process-node.test.js
robobun caf19d4
child_process: use the captured Object.hasOwn in the catch of spawn()
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
Some comments aren't visible on the classic Files Changed page.
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
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,94 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
| import { EventEmitter } from "node:events"; | ||
| import os from "node:os"; | ||
| import util from "node:util"; | ||
|
|
||
| // Node assigns `code` (and the warning's name/code/detail) as plain enumerable own | ||
| // properties (lib/internal/errors.js, lib/internal/process/warning.js). Structured | ||
| // loggers and `JSON.stringify` rely on that. | ||
|
|
||
| function capture(fn: () => unknown): any { | ||
| try { | ||
| fn(); | ||
| } catch (e) { | ||
| return e; | ||
| } | ||
| throw new Error("did not throw"); | ||
| } | ||
|
|
||
| test("ERR_* code is an own enumerable property", () => { | ||
| const e = capture(() => Buffer.alloc(-1)); | ||
| expect(e.code).toBe("ERR_OUT_OF_RANGE"); | ||
| expect(Object.getOwnPropertyDescriptor(e, "code")).toEqual({ | ||
| value: "ERR_OUT_OF_RANGE", | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| expect(Object.keys(e)).toEqual(["code"]); | ||
| const forIn: string[] = []; | ||
| for (const k in e) forIn.push(k); | ||
| expect(forIn).toEqual(["code"]); | ||
| expect(JSON.stringify(e)).toBe('{"code":"ERR_OUT_OF_RANGE"}'); | ||
| expect({ ...e }.code).toBe("ERR_OUT_OF_RANGE"); | ||
| expect(util.inspect(e)).toContain("code: 'ERR_OUT_OF_RANGE'"); | ||
| }); | ||
|
|
||
| test("ERR_* code comes before the extra properties a site adds", () => { | ||
| const e = capture(() => new EventEmitter().emit("error", 42)); | ||
| expect(e.code).toBe("ERR_UNHANDLED_ERROR"); | ||
| expect(Object.keys(e)).toEqual(["code", "context"]); | ||
| expect(JSON.parse(JSON.stringify(e))).toEqual({ code: "ERR_UNHANDLED_ERROR", context: 42 }); | ||
| }); | ||
|
|
||
| test("ERR_INVALID_ARG_VALUE RangeError carries an own enumerable code", () => { | ||
| const e = capture(() => process.cpuUsage({ user: -1, system: 0 })); | ||
| expect(e.name).toBe("RangeError"); | ||
| expect(e.code).toBe("ERR_INVALID_ARG_VALUE"); | ||
| expect(Object.keys(e)).toEqual(["code"]); | ||
| }); | ||
|
|
||
| test("ERR_* code is writable and deletable like in node", () => { | ||
| const e = capture(() => Buffer.alloc(-1)); | ||
| e.code = "CUSTOM"; | ||
| expect(e.code).toBe("CUSTOM"); | ||
| // Node's toString closes over the original code instead of reading this.code. | ||
| expect(e.toString()).toStartWith("RangeError [ERR_OUT_OF_RANGE]: "); | ||
| delete e.code; | ||
| expect(Object.hasOwn(e, "code")).toBe(false); | ||
| expect(e.code).toBeUndefined(); | ||
| expect(e.toString()).toStartWith("RangeError [ERR_OUT_OF_RANGE]: "); | ||
| }); | ||
|
|
||
| test("ERR_SYSTEM_ERROR code is enumerable next to info, errno and syscall", () => { | ||
| const e = capture(() => os.setPriority(-1, 0)); | ||
| expect(e.code).toBe("ERR_SYSTEM_ERROR"); | ||
| expect(Object.keys(e).sort()).toEqual(["code", "errno", "info", "syscall"]); | ||
| }); | ||
|
|
||
| test("warning objects expose name, code and detail as enumerable properties", async () => { | ||
| // A separate process: other test files in the same run can replace the "warning" listeners. | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "--no-warnings", | ||
| "-e", | ||
| `const seen = []; | ||
| process.on("warning", w => seen.push({ keys: Object.keys(w), json: JSON.parse(JSON.stringify(w)) })); | ||
| process.emitWarning("w", { code: "MY_CODE", detail: "d" }); | ||
| // Like Node, an Error passed in is emitted as is: no name, code or detail are added to it. | ||
| process.emitWarning(new Error("e"), { type: "DeprecationWarning", code: "DEP0199", detail: "d" }); | ||
| process.on("exit", () => console.log(JSON.stringify(seen)));`, | ||
| ], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(JSON.parse(stdout)).toEqual([ | ||
| { keys: ["name", "code", "detail"], json: { name: "Warning", code: "MY_CODE", detail: "d" } }, | ||
| { keys: [], json: {} }, | ||
| ]); | ||
| expect(exitCode).toBe(0); | ||
| }); |
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.