Skip to content

Custom properties on native Error objects thrown from a worker thread are lost #1143

Description

@doff6

Description

Just see the reproduction below.

Reproduction

worker.js:

import { serializeError } from "serialize-error";

export function fn1() {
  throw Object.assign(new Error("ERROR"), { foo: "bar" });
}

export function fn2() {
  // `serializeError()` converts a native Error object into a non-native one.
  throw serializeError(Object.assign(new Error("ERROR"), { foo: "bar" }));
}

main.js:

import path from "path";
import { pathToFileURL } from "url";
import assert from "assert";
import Piscina from "piscina";

const worker = new Piscina({
  filename: pathToFileURL(path.join(import.meta.dirname, "worker.js")).href,
});

try {
  await worker.run(null, { name: "fn1" });
} catch (error) {
  assert.strictEqual(error.message, "ERROR");
  // Custom properties are unexpectedly lost.
  assert.strictEqual(error.foo, undefined);
  assert.deepStrictEqual(Object.keys(error), []);
}

try {
  await worker.run(null, { name: "fn2" });
} catch (error) {
  assert.strictEqual(error.message, "ERROR");
  // Custom properties are reserved.
  assert.strictEqual(error.foo, "bar");
  // Native error properties are properly handled by `serializeError()` as well.
  assert.deepStrictEqual(Object.keys(error), [
    "foo",
    "name",
    "message",
    "stack",
  ]);
}

Environment

  • OS: WSL1 on Windows 10 22H2
  • Node.js version: 26.5.0
  • Piscina version: 5.3.2

Expected Behavior

Of course, those custom properties should be perfectly reserved. Certain properties of an error object are often crucial to the logic of the code; for example, the error.code property in better-sqlite3:
https://github.com/WiseLibs/better-sqlite3/blob/f8e2d541208281368129929a96f70f937c0735ef/lib/sqlite-error.js#L10
I stumbled across this issue by chance. For others who haven’t noticed it yet, this has likely caused logical errors in their code without them realising it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions