I'm writing a data processing system in Node.js.
Pieces of data are user provided or come from outside sources, so
In those cases I generally want to alert user about it and then:
Of course the method of alerting user also depends on mode.
I thought that exceptions are right solution for this kind of problem, because:
So I started to look for guides and found two relevant things:
The former is without further explanation. The later I understand, but I think it's not my case.
How would you address this? I'm looking for general approach, not necessarily platform or language specific... or is there anything especially evil in JavaScript try-catch?
It is true that try { } catch { } finally { } has limited usefulness in standard node, and error handling can be problematic.
However In a project I was recently on, I used these node modules: Fibers, Fibers-Promise in a way where effectively, I could do something akin to Thread.join on async callbacks, and making it possible to use node in a procedural, rather than functional style.
There are tradeoffs. Fibers / all co-routine libraries modify node's core code, making it impossible to use on the front end. But depending on your purposes, you might want to look into this.
If it is DATA that you are handling, you might try to think of the app as a message transport.
In this pattern, there cannot be anything to catch, any data that is not understood has a valid message returned to the user - a message saying that the data was invalid.
That way you are not trying to use your code to catch a data error - not something that is good to do.
You don't really need a special library to handle this, it is as much a thought exercise as anything. Actually, a "Model" handler (think MVC) would do the job nicely. Alternatively, you simply need to filter out any really bad data (zero or excessively long for example) before passing it to a parser function - if the parser fails to fully understand the data, default to returning an error message to the user.