Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Emit SAXErrors and allow consumer to handle or bail on SAXErrors

  • Loading branch information...
commit ebccdcc36e8e0ebf039cfeac65b8e23e6ab365ae 1 parent bdb339e
Dan MacTough authored

Showing 1 changed file with 11 additions and 9 deletions. Show diff stats Hide diff stats

  1. +11 9 main.js
20 main.js
@@ -43,7 +43,7 @@ function FeedParser (options) {
43 43 this.parseOpts(options);
44 44 // See https://github.com/isaacs/sax-js for more info
45 45 this.stream = sax.createStream(this.options.strict /* strict mode - no by default */, {lowercase: true, xmlns: true });
46   - this.stream.on('error', this.handleError.bind(this, this.handleSaxError.bind(this)));
  46 + this.stream.on('error', this.handleSaxError.bind(this));
47 47 this.stream.on('processinginstruction', this.handleProcessingInstruction.bind(this));
48 48 this.stream.on('opentag', this.handleOpenTag.bind(this));
49 49 this.stream.on('closetag',this.handleCloseTag.bind(this));
@@ -84,6 +84,7 @@ FeedParser.prototype.parseOpts = function (options) {
84 84 if (!('strict' in this.options)) this.options.strict = false;
85 85 if (!('normalize' in this.options)) this.options.normalize = true;
86 86 if (!('addmeta' in this.options)) this.options.addmeta = true;
  87 + if (!('resume_saxerror' in this.options)) this.options.resume_saxerror = true;
87 88 if ('MAX_BUFFER_LENGTH' in this.options) {
88 89 sax.MAX_BUFFER_LENGTH = this.options.MAX_BUFFER_LENGTH; // set to Infinity to have unlimited buffers
89 90 } else {
@@ -124,20 +125,21 @@ FeedParser.prototype.handleEnd = function (){
124 125 this.stream._parser.close();
125 126 };
126 127
127   -FeedParser.prototype.handleSaxError = function (){
  128 +FeedParser.prototype.handleSaxError = function (e) {
  129 + this.emit('error', e);
  130 + if (this.options.resume_saxerror) {
  131 + this.resumeSaxError();
  132 + }
  133 +};
  134 +
  135 +FeedParser.prototype.resumeSaxError = function () {
128 136 if (this.stream._parser) {
129 137 this.stream._parser.error = null;
130 138 this.stream._parser.resume();
131 139 }
132 140 };
133 141
134   -FeedParser.prototype.handleError = function (next, e){
135   - // A SaxError will prepend an error-handling callback,
136   - // but other calls to #handleError will not
137   - if (next && !e) {
138   - e = next;
139   - next = null;
140   - }
  142 +FeedParser.prototype.handleError = function (e){
141 143 // Only emit the error event if we are not using CPS or
142 144 // if we have a listener on 'error' even if we are using CPS
143 145 if (!this.silenceErrors && (!this.callback || this.listeners('error').length)) {

0 comments on commit ebccdcc

Please sign in to comment.
Something went wrong with that request. Please try again.