I need to perform a series of transformations from a source of data. In the event that an error is emitted while data is being sent through the pipeline, I need to handle the error appropriately. Instead the response just hangs.
The only way I can close the hanging response is by calling outStream.close(<string message here>)
, but that isn't much use because I would like to actually catch the error being emitted.
const seriesOfPipes = async () => {
try {
const streams = [transformStream1, transformStream2, transformStream3];
const res = await fetch("foo");
let outStream = res.body;
for (const streamItem of streams) {
outStream.on('error', streamItem.destroy.bind(streamItem));
outStream = outStream.pipe(streamItem);
}
return outStream;
} catch (e) {
// the error doesn't appear
console.log("you caught the error ", e.message);
}
};
My response was hanging because the response handler was not closing, I should've realized that from the start. Also, the bind
was not necessary. So really what the error event handler should be doing is
outStream.on('error', (err) => {
// sanitize error here
const displayErr = sanitizeError(err);
// pass displayErr to express response object
resObj.writeHead(statusCode).end(JSON.stringify(displayErr);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.