Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.89k stars 605 forks source link

Async parseString hides exception #293

Open mcheshkov opened 8 years ago

mcheshkov commented 8 years ago
var xml2js = require('xml2js');
xml2js.parseString("<a></a>", {async: true}, function(err, res){
    throw new Error("test");
});

This script does not show any unhandled errors.

Leonidas-from-XIV commented 8 years ago

This is due telling Node to call the processing function via setImmediate which calls it when the scheduler decides it's time, with no way to pass the exception back to the code that called parseString in the first place.

If you would like to get that exception, you could have an error handler either in the callback directly or call an error callback from a catch block in the callback.

mcheshkov commented 8 years ago

I don't want to catch exception, I want proper unhandled exception.

This script

setImmediate(function(){
    throw new Error("test");
});

Gives me this in terminal

$ node a && echo "Good" || echo "Bad"
/home/cheshkov/xlsx_temp/a.js:6
    throw new Error("test");
    ^

Error: test
    at Immediate._onImmediate (a.js:6:8)
    at processImmediate [as _immediateCallback] (timers.js:383:17)
Bad

But xml2js comsume this exception, and leave no trace

var xml2js = require('xml2js');
xml2js.parseString("<a></a>", {async: true}, function(err, res){
    throw new Error("test");
});
$ node a && echo "Good" || echo "Bad"
Good