Node exits when an error occurs using withPromise
pascalgn opened this issue ยท 2 comments
pascalgn commented
See this example file:
const { fdir } = require("fdir");
async function main() {
const files = await new fdir()
.withBasePath()
.crawl("non-existent") // directory does not exist!
.withPromise();
console.log("this will never be written");
}
if (require.main === module) {
main().catch((e) => {
process.exitCode = 1;
console.error(e);
});
}
(save as index.js
, for example, and run node ./index.js
)
What is happening?
The Node.js process just exits (exit code 0) and no error is shown. The message "this will never be written" is also not shown.
What is expected to happen?
As the docs say that all errors are suppressed, I would expect files
to be an empty array and program flow to continue normally (printing the "this will never be written" message).
Have you gone mad trying to figure out why your program just exits without any message?
I prefer not to say.
Environment
$ node -v
v15.4.0
$ cat node_modules/fdir/package.json | grep version
"version": "4.1.0",
Is there a workaround?
When adding .withErrors()
, the error is reported as expected ("ENOENT: no such file or directory, scandir 'non-existent'") so using that option, together with a try-catch or .catch() could be a possible workaround.