每次我的 Stream 代码出错时,我都会尝试抛出它并捕获它以丢弃流。 为什么每次我抛出错误时代码都会崩溃而没有抓住它?
.on('error', (err) => {
console.log('error');
// console.log(err);
throw err;
})
.on('end', () => {
console.log('end');
next();
})
.on('close', () => {
console.log('close');
next();
});
} catch (error) {
console.log('err2');
next(error);
} finally {
fs.unlink(filePath, (err) => {
if (err) {
throw err;
}
});
}
};
控制台日志:
/Users/vlad-pc/git/gis_backend/middleware/csvMiddleware.js:68
throw err;
^
Error: There Spacing on row 7 File Uploud Stop
at module.exports (/Users/vlad-pc/git/gis_backend/utils/error.js:2:17)
at /Users/vlad-pc/git/gis_backend/middleware/csvMiddleware.js:56:15
at Array.map (<anonymous>)
at ReadStream.<anonymous> (/Users/vlad-pc/git/gis_backend/middleware/csvMiddleware.js:39:30)
at ReadStream.emit (events.js:310:20)
at addChunk (_stream_readable.js:286:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:209:10)
at internal/fs/streams.js:210:12
at FSReqCallback.wrapper [as oncomplete] (fs.js:488:5) {
statusCode: 400
}
[nodemon] app crashed - waiting for file changes before starting...