aseemk / requireDir

Node.js helper to require() directories.
MIT License
484 stars 60 forks source link

How to handle errors in required file? #63

Open Arcanorum opened 3 years ago

Arcanorum commented 3 years ago

Some of my files have erroneous code in them. How can I catch the exceptions thrown by these during the require? Currently I can only try-catch the whole require-dir, not on a file by file basis.

yocontra commented 3 years ago

We could add an onError option that would wrap https://github.com/aseemk/requireDir/blob/master/index.js#L128 in a try/catch - why do you need to catch it per-file?

Arcanorum commented 3 years ago

Yes that is basically what I have done for now. I have a case where I have some (or a lot) of files in the directory I'm requiring that now throw errors due to changes I've made elsewhere, but I still want to test fixing some of them to see if my changes work for those, then update the rest of the erroring files, but to catch those errors for the time being so they don't crash the app, until I fix the rest of them.

IK I can just require the individual files that I'm testing the fixes for, but this would just be a nice feature.

yocontra commented 3 years ago

It seems like a fine option to add and should only add a few lines of code - feel free to send a PR that includes docs + tests.