Open samskeller opened 7 years ago
Took me some time to figure out why my assert tests stalls, I has something todo with the error handling in this module.
When async === true, the asserts tests stalls, when 'false' the AssertionErrors are thrown as expected
Example: async: false, works as expected
const assert = require('assert')
const x2j = require('xml2js')
var parser = new x2j.Parser({
async: false
})
parser.parseString('<xml><foo></foo></xml>', (e, o) => {
assert.equal(true, false)
})
process.stdin.resume()
example asyn: true, assert test stalls
const assert = require('assert')
const x2j = require('xml2js')
var parser = new x2j.Parser({
async: true
})
parser.parseString('<xml><foo></foo></xml>', (e, o) => {
assert.equal(true, false)
})
process.stdin.resume()
I'm using
node-xml2js
via theexpress-xml-bodyparser
module, which just allows an express web application to use thenode-xml2js
parser via some middleware for XML requests to convert them to JSON objects. I noticed that when I send an XML body that has two different syntax errors in it, a global error handling function within my express application will catch an error being raised during the parsing of XML, but after that error handling finishes, another error is thrown inxml2js
which causes my express application to fail. An example of a bad XML body would be:where there
<data>
root node is missing, and there's a non-whitespace character before what the parser considers to be the root node.The error that crashes the application looks like this:
However, my application doesn't crash when I have the
async=true
option set -- everything works fine there. From digging into thenode-xml2js
code, it looks like the error handling with asynchronous parsing is different than it is whenasync=false
. Here's the relevant code fromParser.prototype.parseString
after an error is caught withthis.saxParser.write(str).close();
: https://github.com/Leonidas-from-XIV/node-xml2js/blob/1ab44ea837eff59305bd11f0e1a1e542e7c3e79f/lib/parser.js#L324-L330However, the error handling when
async=true
, fromParser.prototype.processAsync
looks like this: https://github.com/Leonidas-from-XIV/node-xml2js/blob/1ab44ea837eff59305bd11f0e1a1e542e7c3e79f/lib/parser.js#L85-L89You can see that the
this.saxParser.ended
is not taken into account, and there's nothrow err;
line. I believe this is the reason why, when I haveasync=true
in my configuration, I don't see the same hard error crashing my application as I do when I haveasync=false
in my configuration.Let me know if I can provide more information! Thanks!