requireDir icon indicating copy to clipboard operation
requireDir copied to clipboard

How to handle errors in required file?

Open Arcanorum opened this issue 3 years ago • 3 comments

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.

Arcanorum avatar Aug 17 '20 17:08 Arcanorum

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?

yocontra avatar Aug 17 '20 21:08 yocontra

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.

Arcanorum avatar Aug 19 '20 20:08 Arcanorum

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.

yocontra avatar Aug 19 '20 23:08 yocontra