I am wondering which approach is better for error handling and debugging in JavaScript for nodeJS?
Having a pure try/catch block:
try
{
dangerous.function();
}
catch(error);
{
console.log(error);
}
or
Just using a function as a parameter, which will display if any errors occur
dangerous.function(function(error)
{
console.log(error);
});
I am raising this question because I read that try/throw has a potential of logging too much stack trace data as written here: http://nodejs.org/api/domain.html#domain_warning_don_t_ignore_errors
It's totally up to you. Exceptions have the advantage of providing a stack trace.
Note that if dangerous.function
does its work asynchronously, then you'll need to use your second approach, since there isn't any exception to catch initially. But that doesn't mean you can't use an exception, just that if you want to use an exception, you have to pass the exception to the callback rather than throwing it. (Or don't use an exception, that's also totally up to you.)
They aimed to fulfill the same purpose, but they are to be used in different contexts:
I strongly advice to read this article (even though it is first addressed for Node.js developers, it is valuable for every JavaScript developer) which fully covers the subject.