Skip to content

Commit

Permalink
feat: drop the resume() method; and have onerror() throw
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Sax had no default error handler but if you wanted to continue calling
``write()`` after an error you had to call ``resume()``. We do away with
``resume()`` and instead install a default ``onerror`` which throws. Replace
with a no-op handler if you want to continue after errors.
  • Loading branch information
lddubeau committed Jul 6, 2018
1 parent 5258939 commit ac601e5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
3 changes: 0 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const parser = saxes.parser();
function inspector(ev) {
return function handler(data) {
console.error("%s %s %j", `${this.line}:${this.column}`, ev, data);
if (ev === "error") {
parser.resume();
}
};
}

Expand Down
23 changes: 6 additions & 17 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ exports.EVENTS = [
const buffers = [
"comment", "openWakaBang", "textNode", "tagName", "doctype", "piTarget",
"piBody", "entity", "attribName", "attribValue", "cdata", "xmlDeclName",
"xmlDeclValue"
"xmlDeclValue",
];

let Stream;
Expand Down Expand Up @@ -142,7 +142,7 @@ class SAXParser {
this.opt = opt || {};
this.tags = [];
this.closed = this.closedRoot = this.sawRoot = false;
this.tag = this.error = null;
this.tag = null;
this.state = S_BEGIN;
this.ENTITIES = Object.create(XML_ENTITIES);
this.attribList = [];
Expand Down Expand Up @@ -188,11 +188,13 @@ class SAXParser {
onopencdata() {}
oncdata() {}
onclosecdata() {}
onerror() {}
onend() {}
onready() {}
onopennamespace() {}
onclosenamespace() {}
onerror(err) {
throw new Error(err);
}
/* eslint-enable class-methods-use-this */

end() {
Expand Down Expand Up @@ -220,15 +222,11 @@ class SAXParser {
const message = (this.trackPosition) ?
`${this.fileName}:${this.line}:${this.column}: ${er}` : er;

this.error = er = new Error(message);
this.onerror(er);
this.onerror(new Error(message));
return this;
}

write(chunk) {
if (this.error) {
throw this.error;
}
if (this.closed) {
return this.fail("cannot write after close; assign an onready handler.");
}
Expand Down Expand Up @@ -1002,11 +1000,6 @@ class SAXParser {
return this;
}

resume() {
this.error = null;
return this;
}

close() {
return this.write(null);
}
Expand Down Expand Up @@ -1316,10 +1309,6 @@ class SAXStream extends Stream {

this._parser.onerror = (er) => {
this.emit("error", er);

// if didn't throw, then means error was handled.
// go ahead and clear error, so we can write again.
this._parser.error = null;
};
}

Expand Down
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ exports.test = function test(options) {

if (ev === "error") {
expect([ev, n.message]).to.deep.equal(expected[expectedIx]);
parser.resume();
}
else {
if (ev === "opentagstart" || ev === "opentag") {
Expand Down

0 comments on commit ac601e5

Please sign in to comment.