Question

I'm modifying an existing Birt report that is parsing XML with JavaScript. The parsing is working fine when the XML isn't Malformed. Now I'm testing it so that if bum XML is passed it, it instead returns the full message. Here is the code:

var xml = fullString.substring(fullString.indexOf("query") + 5);
importPackage(Packages.org.apache.xerces.parsers);
importPackage(Packages.org.xml.sax);
importPackage(Packages.java.io);

var entry = "";

try{
    var parser = new DOMParser();
    parser.parse(new InputSource(new StringReader(xml)));
    var nodelist = parser.getDocument().getElementsByTagName('parsererror');
    if(nodelist.length !== 0){
        entry = fullString;
    }else{
        nodelist = parser.getDocument().getElementsByTagName('ProprietaryTag');
        for(var i = 0; i < nodelist.length; i++){
            entry = entry + nodelist.item(i).getFirstChild().getNodeValue() + " ";
        }
    }
}catch(err){
    entry = fullString;
}

The weird thing is that the page renders and works but the logs still get the following

2014-04-29 11:57:30,676 ERROR [STDERR] (http-0.0.0.0-58880-13) [Fatal Error] :1:94: Element type "asdfwefe" must be followed by either attribute specifications, ">" or "/>".

Which is the proper error, but my understanding from try/catch in other languages is that its supposed to prevent that. Why does the error get thrown to the logs even though it was in a try catch?

Was it helpful?

Solution

Logging an exception and throwing an exception are two distinct things, it is quite common for a library component (xerces in this case) to log a message describing the error encountered and then throw an exception, regardless if the exception is caught or not by calling programs.

In your case adding this line should prevent Xerces from logging:

parser.setErrorHandler(null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top