Repository navigation
node errors: give AbortError a real constructor so util.inspect prints its class name #39300
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
34d14b0
node errors: give AbortError a real constructor so util.inspect print…
robobun 41167d0
Validate AbortError options with node's typeof check
robobun 71356c4
Shorten the ABORT_ERR constructor comment
robobun 793c4f7
Cover bigint and symbol options in the AbortError constructor test
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
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,143 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { EventEmitter, once } from "node:events"; | ||
| import { readFile } from "node:fs/promises"; | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
| import { inspect } from "node:util"; | ||
|
|
||
| // Node's AbortError is `class AbortError extends Error` (lib/internal/errors.js), so | ||
| // `err.constructor` is a function named AbortError. Bun builds these errors natively | ||
| // (ErrorCode.cpp) from two entry points, `$makeAbortError` in the JS builtins and | ||
| // `Bun__wrapAbortError` for Rust callers; both must produce the same class shape. | ||
| // Except for the bun:test formatting case, every assertion here also holds on Node v26. | ||
|
|
||
| async function rejection(promise: Promise<unknown>): Promise<any> { | ||
| try { | ||
| await promise; | ||
| } catch (e) { | ||
| return e; | ||
| } | ||
| throw new Error("expected the operation to reject"); | ||
| } | ||
|
|
||
| function thrown(fn: () => unknown): any { | ||
| try { | ||
| fn(); | ||
| } catch (e) { | ||
| return e; | ||
| } | ||
| throw new Error("expected the operation to throw"); | ||
| } | ||
|
|
||
| async function abortErrors() { | ||
| const signal = AbortSignal.abort(); | ||
| return { | ||
| // $makeAbortError (JS builtins) | ||
| "timers/promises": await rejection(sleep(1, undefined, { signal })), | ||
| "events.once": await rejection(once(new EventEmitter(), "never", { signal })), | ||
| // Bun__wrapAbortError (Rust callers) | ||
| "fs.promises.readFile": await rejection(readFile(import.meta.path, { signal })), | ||
| "Bun.spawn": thrown(() => Bun.spawn({ cmd: ["this-command-is-never-spawned"], signal })), | ||
| }; | ||
| } | ||
|
|
||
| describe("node-style AbortError", () => { | ||
| test("util.inspect prints it under its own class name", async () => { | ||
| const errors = await abortErrors(); | ||
| const firstLines = Object.fromEntries( | ||
| Object.entries(errors).map(([producer, err]) => [producer, inspect(err).split("\n")[0]]), | ||
| ); | ||
| expect(firstLines).toEqual({ | ||
| "timers/promises": "AbortError: The operation was aborted", | ||
| "events.once": "AbortError: The operation was aborted", | ||
| "fs.promises.readFile": "AbortError: The operation was aborted", | ||
| "Bun.spawn": "AbortError: The operation was aborted", | ||
| }); | ||
| }); | ||
|
|
||
| test("every producer shares one AbortError class", async () => { | ||
| const errors = await abortErrors(); | ||
| const AbortError = errors["timers/promises"].constructor; | ||
| const prototype = Object.getPrototypeOf(errors["timers/promises"]); | ||
|
|
||
| for (const err of Object.values(errors)) { | ||
| expect(err.constructor).toBe(AbortError); | ||
| expect(err).toBeInstanceOf(AbortError); | ||
| expect(Object.getPrototypeOf(err)).toBe(prototype); | ||
| } | ||
|
|
||
| expect(AbortError).not.toBe(Error); | ||
| expect({ name: AbortError.name, length: AbortError.length }).toEqual({ name: "AbortError", length: 0 }); | ||
| expect(AbortError.prototype).toBe(prototype); | ||
| expect(Object.getPrototypeOf(AbortError)).toBe(Error); | ||
| expect(Object.getPrototypeOf(prototype)).toBe(Error.prototype); | ||
|
|
||
| // Same attributes `class AbortError extends Error {}` would produce. | ||
| const { value: constructorValue, ...constructorAttributes } = Object.getOwnPropertyDescriptor( | ||
| prototype, | ||
| "constructor", | ||
| )!; | ||
| expect(constructorValue).toBe(AbortError); | ||
| expect(constructorAttributes).toEqual({ writable: true, enumerable: false, configurable: true }); | ||
| const { value: prototypeValue, ...prototypeAttributes } = Object.getOwnPropertyDescriptor(AbortError, "prototype")!; | ||
| expect(prototypeValue).toBe(prototype); | ||
| expect(prototypeAttributes).toEqual({ writable: false, enumerable: false, configurable: false }); | ||
| }); | ||
|
|
||
| test("err.constructor constructs AbortErrors", async () => { | ||
| const { "timers/promises": err } = await abortErrors(); | ||
| const AbortError = err.constructor; | ||
| const cause = new Error("why"); | ||
|
|
||
| const custom = new AbortError("custom message", { cause }); | ||
| expect(custom).toBeInstanceOf(AbortError); | ||
| expect({ name: custom.name, code: custom.code, message: custom.message, cause: custom.cause }).toEqual({ | ||
| name: "AbortError", | ||
| code: "ABORT_ERR", | ||
| message: "custom message", | ||
| cause, | ||
| }); | ||
| expect(inspect(custom).split("\n")[0]).toBe("AbortError: custom message"); | ||
|
|
||
| const defaults = new AbortError(); | ||
| expect(defaults).toBeInstanceOf(AbortError); | ||
| expect({ name: defaults.name, code: defaults.code, message: defaults.message, cause: defaults.cause }).toEqual({ | ||
| name: "AbortError", | ||
| code: "ABORT_ERR", | ||
| message: "The operation was aborted", | ||
| cause: undefined, | ||
| }); | ||
|
|
||
| const invalid = thrown(() => new AbortError("message", "not an object")); | ||
| expect({ name: invalid.name, code: invalid.code, message: invalid.message }).toEqual({ | ||
| name: "TypeError", | ||
| code: "ERR_INVALID_ARG_TYPE", | ||
| message: `The "options" argument must be of type object. Received type string ('not an object')`, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| test("ERR_INVALID_ARG_TYPE describes it as an instance of AbortError", async () => { | ||
| const { "fs.promises.readFile": err } = await abortErrors(); | ||
| const invalid = thrown(() => Buffer.from(err)); | ||
| expect({ code: invalid.code, message: invalid.message }).toEqual({ | ||
| code: "ERR_INVALID_ARG_TYPE", | ||
| message: | ||
| "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of AbortError", | ||
| }); | ||
| }); | ||
|
|
||
| test("bun:test formats it under its class name, like jest does for node's AbortError", async () => { | ||
| const { "events.once": err } = await abortErrors(); | ||
| expect(err).toMatchInlineSnapshot(`[AbortError: The operation was aborted]`); | ||
| }); | ||
|
|
||
| test("ERR_* errors still report the base constructor, like node", () => { | ||
| // Node keeps `constructor` pointing at the base class for its ERR_* codes; only | ||
| // AbortError is a class of its own. | ||
| const err = thrown(() => Buffer.from(1 as any)); | ||
| expect({ code: err.code, constructor: err.constructor, name: err.name }).toEqual({ | ||
| code: "ERR_INVALID_ARG_TYPE", | ||
| constructor: TypeError, | ||
| name: "TypeError", | ||
| }); | ||
| }); | ||
| }); | ||
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.