I have a basic code reading from a file, and I want to handle errors such as not being able to open the file. Here's what my code looks like:
async function processFile() {
const fileStream = fs.createReadStream(source);
fileStream.on('error', function(err) {
console.log("An error occured while opening the file")
throw err
//return Promise.reject(err)
//return
});
}
async function main(){
try{
await processFile();
} catch(err){
console.error("catching error")
return
}
}
main()
I'm typically getting this result:
An error occured while opening the file
catching error
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
[Error: ENOENT: no such file or directory, open 'source.tx'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'source.tx'
}
Node.js v19.2.0
So as you can see, both custom messages are displayed properly, but I am still getting that error block, whether I'm using throw or reject()
What is the issue with the code and how can I solve it?
Thank you.