Every function create new Error Object. So how I can get the preivous errors? For example this is my code:
function main(callback) {
a(function(err) {
if (err) {
callback(new Error('cannot run main function..'));
return
}
})
}
function a(callback) {
b(function(err) {
if (err) {
callback(new Error('cannot run b function'));
return
}
})
}
function b(callback) {
if (1 == 2) callback(new Error('Error in b function'))
}
When I run this, I get only the last error ("cannot run main function') but I want to get all the previous errors. Do you have any best practive for that?
What I am doing is this:
if(err){
err=new Error('cannot run this function.\r' + err.message)
}
I am asking, of you know about any other library or better way for doing that. Something that extend the Error object...
Something like:
err.push(new Error('...'))
There is no "standard" way of doing something like this in Node.js yet. There are various libraries that do what you're describing. The best method I have found is to create a custom error object that extends the default JS Error
object, and then define additional methods to it for your purposes. I wrote the x-error library that does just this, you can either use it and extend it (see bottom of README for how to extend it), or just take a look at its code (which is straightforward) and roll your own custom error object implementation.
Note: x-error has a debug
method that is similar to what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With