Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 602 forks source link

throw new Error("Uncaught, unspecified 'error' event."); #188

Open askie opened 9 years ago

askie commented 9 years ago
events.js:73
        throw new Error("Uncaught, unspecified 'error' event.");
              ^
Error: Uncaught, unspecified 'error' event.
    at Parser.EventEmitter.emit (events.js:73:15)
    at Parser.exports.Parser.Parser.parseString (/mnt/hgfs/wwwroot/railway/node_modules/xml2js/lib/xml2js.js:451:16)
    at Parser.bind [as parseString] (/mnt/hgfs/wwwroot/railway/node_modules/xml2js/lib/xml2js.js:6:59)
    at Object.exports.parseString (/mnt/hgfs/wwwroot/railway/node_modules/xml2js/lib/xml2js.js:479:19)
Leonidas-from-XIV commented 9 years ago

Can you post a script and XML that reproduces this error?

danielravina commented 9 years ago

I'm getting the same error

Script

xml2js     = require "xml2js"
parser = new xml2js.Parser()
 _pullBookings: (params, callback) ->
    body = require("./resources/xmlPostBody")(params)

    postRequest =
      host: "..."
      path: "..."
      method: "POST"
      port: 80
      SOAPAction: "..."
      headers:
        'Content-Type': 'text/xml'
        'Content-Length': Buffer.byteLength(body)

    req = https.request postRequest, (res) ->
      buffer = "";
      res.on "data", (data) ->
        buffer = buffer + data;
      res.on "end", ->
        parser.parseString buffer, (err, result) -> # error happens here
          XMLStringResponse = result['soap:Envelope']['soap:Body'][0]['GetAllBookingsResponse'][0]['GetAllBookingsResult'][0]
          if (XMLStringResponse.length > 12) # probably not empty
            parser.parseString XMLStringResponse , (err, result) ->
              callback(result)
          else
            callback()

    req.on 'error', (e) ->
      logger.log 'problem with EMS request: ' + e.message
    req.write( body );
    req.end();

XML

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetAllBookingsResponse xmlns="http://DEA.EMS.API.Web.Service/"><GetAllBookingsResult>&lt;Bookings /&gt;</GetAllBookingsResult></GetAllBookingsResponse></soap:Body></soap:Envelope>

thanks

Lekensteyn commented 9 years ago

If your callback somehow throws an exception which is not instanceof Error, you can get this.

parseString(buffer, function(err, result) {
    // this is problematic
    throw 'this is unspecified';
    // use this instead
    throw new Error('this is specified');
});
nkorth commented 9 years ago

I got this issue even though I'm not throwing any exceptions at all in my callback - I just had a ReferenceError, and it was a pain in the ass to track down since all the info I got was "Uncaught, unspecified 'error' event.".

And I did see the attempted fix in #138 - I'm on version 0.4.9 and it's still broken.