Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/stream-transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ Transformer.prototype._transform = function (chunk, _, cb) {
// sync
const result = this.handler.call(this, chunk, this.options.params);
if (result && result.then) {
result.then((result) => {
this.__done(null, [result], cb);
});
result.catch((err) => {
this.__done(err);
});
result.then(
(result) => {
this.__done(null, [result], cb);
},
(err) => {
this.__done(err);
},
);
} else {
this.__done(null, [result], cb);
}
Expand Down
47 changes: 47 additions & 0 deletions packages/stream-transform/test/handler.promise.error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";

describe("handler.promise.error", function () {
for (const api of ["callback", "pipeline"]) {
it(`handles rejected promises with the ${api} API`, function () {
const result = spawnSync(
process.execPath,
[
"--unhandled-rejections=strict",
"--input-type=module",
"--eval",
`
import assert from "node:assert/strict";
import { Readable, Writable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { transform } from ${JSON.stringify(new URL("../lib/index.js", import.meta.url).href)};

const error = new Error("Catchme");
const handler = async (record) => {
throw error;
};
if (${JSON.stringify(api)} === "callback") {
await new Promise((resolve) => {
transform([["value"]], handler, (err) => {
assert.equal(err, error);
resolve();
});
});
} else {
await assert.rejects(
pipeline(
Readable.from([["value"]]),
transform(handler),
new Writable({ objectMode: true, write: (_, __, next) => next() }),
),
(err) => err === error,
);
}
`,
],
{ encoding: "utf8" },
);
assert.equal(result.status, 0, result.stderr);
});
}
});