Skip to content

Commit

Permalink
Improved parser error handling.
Browse files Browse the repository at this point in the history
Show output error position. Resolves #482
  • Loading branch information
GreLI committed Feb 7, 2016
1 parent e21f8d0 commit 9cdb046
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
2 changes: 0 additions & 2 deletions lib/svgo.js
Expand Up @@ -51,7 +51,6 @@ SVGO.prototype.optimize = function(svgstr, callback) {
};

SVGO.prototype._optimizeOnce = function(svgstr, callback) {

var config = this.config;

SVG2JS(svgstr, function(svgjs) {
Expand All @@ -66,7 +65,6 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) {
callback(JS2SVG(svgjs, config.js2svg));

});

};

/**
Expand Down
16 changes: 12 additions & 4 deletions lib/svgo/svg2js.js
Expand Up @@ -10,7 +10,7 @@ var config = {
normalize: true,
lowercase: true,
xmlns: true,
position: false
position: true
};

/**
Expand Down Expand Up @@ -142,17 +142,25 @@ module.exports = function(data, callback) {

sax.onerror = function(e) {

callback({ error: 'Error in parsing: ' + e.message });
e.message = 'Error in parsing SVG: ' + e.message;

};

sax.onend = function() {

if (!this.error) callback(root);
if (!this.error) {
callback(root);
} else {
callback({ error: this.error.message });
}

};

sax.write(data).close();
try {
sax.write(data).close();
} catch (e) {
callback({ error: e.message });
}

function trim(elem) {
if (!elem.content) return elem;
Expand Down
23 changes: 5 additions & 18 deletions test/svg2js/_index.js
Expand Up @@ -420,29 +420,16 @@ describe('svg2js', function() {
return root.error.should.an.instanceOf(String);
});

it('should be "Error in parsing: Unmatched closing tag: style"', function() {
return root.error.should.equal('Error in parsing: Unmatched closing tag: style');
it('should be "Error in parsing SVG: Unmatched closing tag: style"', function() {
return root.error.should.equal('Error in parsing SVG: Unmatched closing tag: style\nLine: 10\nColumn: 15\nChar: >');
});

});

describe('thrown error', function() {
describe('error', function() {

it('should be an instance of Error', function() {
return error.should.be.an.instanceOf(Error);
});

it('should stringify to "Error: Unmatched closing tag: style"', function() {
return error.toString().should.equal('Error: Unmatched closing tag: style');
});

it('should roughly match root.error', function() {
var rootErrorParts = root.error.split(':'),
thrownErrorParts = error.toString().split(':');

return rootErrorParts.reduce(function(result, value, index) {
return result && value.indexOf(thrownErrorParts[index]) === 0;
}, true).should.be.true();
it('should not be thrown', function() {
return typeof error === 'undefined';
});

});
Expand Down

0 comments on commit 9cdb046

Please sign in to comment.