danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

How to catch Sax errors? #109

Open OliverJAsh opened 10 years ago

OliverJAsh commented 10 years ago

Sometimes I get sax errors. How can I catch them?

I have kept resume_saxerror to its default value of true.

Error: Unexpected end
Line: 543
Column: 146
Char:
                at error (/Users/Oliver/Development/sbscribe/sbscribe-common/node_modules/feedparser/node_modules/sax/lib/sax.js:642:8)
                at end (/Users/Oliver/Development/sbscribe/sbscribe-common/node_modules/feedparser/node_modules/sax/lib/sax.js:650:64)
                at SAXParser.end (/Users/Oliver/Development/sbscribe/sbscribe-common/node_modules/feedparser/node_modules/sax/lib/sax.js:149:24)
                at SAXStream.end (/Users/Oliver/Development/sbscribe/sbscribe-common/node_modules/feedparser/node_modules/sax/lib/sax.js:234:16)
                at FeedParser._flush (/Users/Oliver/Development/sbscribe/sbscribe-common/node_modules/feedparser/main.js:1012:17)
                at FeedParser.<anonymous> (_stream_transform.js:130:12)
                at g (events.js:180:16)
                at EventEmitter.emit (events.js:92:17)
danmactough commented 10 years ago

When resume_saxerror is true, they are emitted on error. Are you not getting them in your error handler?

OliverJAsh commented 10 years ago

Regardless of whether I have resume_saxerror set to true or false, I am getting an uncaught exception – despite the fact I have an error event listener.

var feedParser = new FeedParser({
    addmeta: false,
    feedurl: feedUrl,
    resume_saxerror: true
});

feedParser.on('error', function () {
    // This is called, but it does not *catch* the error.
    console.log('feedParser error', arguments);
});
OliverJAsh commented 10 years ago

Bump.

danmactough commented 10 years ago

@OliverJAsh I'm working on refactoring SAX error handling, which I believe is actually a bit broken in sax. Still digging.

Your code about doesn't have a parameter for the error handler. Are you sure it's being triggered by the error that gets thrown? Or that the "Unexpected end" error after you've already handled the other error, possibly.

OliverJAsh commented 10 years ago

It is being triggered by the error that gets thrown because I am seeing the error logged to the console (I just log the arguments which contains the error).

OliverJAsh commented 10 years ago

However, it is immediately followed by the uncaught exception, from the same error.

danmactough commented 10 years ago

That sounds like the error is not getting cleared for some reason. If sax resumes parsing and the error has not been cleared, it will throw -- intentionally throw.

I cannot reproduce this behavior, though. Can you post a gist or something with runnable code that does this so I can help you debug it?

OliverJAsh commented 10 years ago

I have this problem when parsing http://theramblingtour.smugglersrecords.com/?feed=rss2 with the following code:

feedParser
    .on('error', function (error) {
        // This should catch the error, but it doesn't.
    })
    .on('readable', function () {
        this.end();
    });

The error only occurs upon this feed when I call this.end() in the readable event. If I don't call that error, it doesn't error – I imagine because we aren't pulling any data through the stream?

If you still can't reproduce it with that information, I’ll have a go at creating an isolated case in a gist for you.

danmactough commented 10 years ago

I did this: https://gist.github.com/danmactough/342c037d8094ce1a553d and cannot reproduce.