Repository navigation
node errors: make .code an own property on ERR_* errors #35926
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33ab16f
node errors: make .code an own property on ERR_* errors
robobun 05b29f3
[autofix.ci] apply automated fixes
autofix-ci[bot] a0790f8
guard created_error against ErrorInstance::create throwing in non-Zig…
robobun 7029322
also fix .code descriptor on SystemError paths
robobun 1a59c33
use setPriority(-1, 0) so the ERR_SYSTEM_ERROR test throws on Windows…
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,95 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { Buffer } from "node:buffer"; | ||
| import { EventEmitter } from "node:events"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
|
|
||
| // Node.js defines `code` on ERR_* errors as an own, enumerable, writable, | ||
| // configurable data property on the instance. That makes it visible to | ||
| // Object.keys, JSON.stringify, spread and hasOwnProperty, which structured | ||
| // error loggers rely on. | ||
|
|
||
| function capture(fn: () => void): Error & { code?: string } { | ||
| try { | ||
| fn(); | ||
| } catch (e) { | ||
| return e as Error & { code?: string }; | ||
| } | ||
| throw new Error("expected throw"); | ||
| } | ||
|
|
||
| describe("Node.js ERR_* error .code is an own property", () => { | ||
| const cases: Array<[string, string, () => void]> = [ | ||
| ["fs options ERR_INVALID_ARG_TYPE", "ERR_INVALID_ARG_TYPE", () => fs.rmSync("/x", { recursive: "yes" as any })], | ||
| ["fs ERR_OUT_OF_RANGE", "ERR_OUT_OF_RANGE", () => fs.mkdirSync("/x", { mode: -1 })], | ||
| ["Buffer ERR_OUT_OF_RANGE", "ERR_OUT_OF_RANGE", () => Buffer.alloc(-1)], | ||
| ["URL ERR_INVALID_URL", "ERR_INVALID_URL", () => new URL("nope")], | ||
| ["events ERR_INVALID_ARG_TYPE", "ERR_INVALID_ARG_TYPE", () => new EventEmitter().setMaxListeners("x" as any)], | ||
| ]; | ||
|
|
||
| test.each(cases)("%s", (_label, expectedCode, fn) => { | ||
| const err = capture(fn); | ||
|
|
||
| expect(err.code).toBe(expectedCode); | ||
|
|
||
| const desc = Object.getOwnPropertyDescriptor(err, "code"); | ||
| expect(desc).toEqual({ | ||
| value: expectedCode, | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(Object.prototype.hasOwnProperty.call(err, "code")).toBe(true); | ||
| expect(Object.keys(err)).toContain("code"); | ||
| expect({ ...err }.code).toBe(expectedCode); | ||
| expect(JSON.parse(JSON.stringify(err)).code).toBe(expectedCode); | ||
| }); | ||
|
|
||
| test("for...in only enumerates own 'code', not prototype name/toString", () => { | ||
| const err = capture(() => Buffer.alloc(-1)); | ||
| const keys: string[] = []; | ||
| for (const k in err) keys.push(k); | ||
| expect(keys).toContain("code"); | ||
| expect(keys).not.toContain("name"); | ||
| expect(keys).not.toContain("toString"); | ||
| }); | ||
|
|
||
| test("prototype does not carry 'code'", () => { | ||
| const err = capture(() => Buffer.alloc(-1)); | ||
| const proto = Object.getPrototypeOf(err); | ||
| expect(Object.prototype.hasOwnProperty.call(proto, "code")).toBe(false); | ||
| }); | ||
|
|
||
| test("toString() still includes the code", () => { | ||
| const err = capture(() => Buffer.alloc(-1)); | ||
| expect(String(err)).toContain("[ERR_OUT_OF_RANGE]"); | ||
| expect(err.name).toBe("RangeError"); | ||
| }); | ||
|
|
||
| test("errno errors have own 'code' with Node's descriptor", () => { | ||
| const err = capture(() => fs.openSync("/nonexistent-robobun-probe", "r")); | ||
| expect(err.code).toBe("ENOENT"); | ||
| for (const prop of ["code", "errno", "syscall", "path"] as const) { | ||
| expect(Object.getOwnPropertyDescriptor(err, prop)).toEqual({ | ||
| value: (err as any)[prop], | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| test("ERR_SYSTEM_ERROR 'code' is own and enumerable", () => { | ||
| const err = capture(() => os.setPriority(-1, 0)); | ||
| expect(err.code).toBe("ERR_SYSTEM_ERROR"); | ||
| expect(Object.getOwnPropertyDescriptor(err, "code")).toEqual({ | ||
| value: "ERR_SYSTEM_ERROR", | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| expect(Object.keys(err)).toContain("code"); | ||
| expect({ ...err }.code).toBe("ERR_SYSTEM_ERROR"); | ||
| }); | ||
| }); |
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.