Skip to content

Commit

Permalink
Leave a noop error handler on the sax stream when aborting due to an …
Browse files Browse the repository at this point in the history
…error

Prevents a parse error from causing an unhandled rejection
  • Loading branch information
papandreou committed May 12, 2019
1 parent 83cf728 commit 26a7019
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/xlsx/xform/base-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ BaseXform.prototype = {
function abort(error) {
// Abandon ship! Prevent the parser from consuming any more resources
parser.removeAllListeners();
parser.on('error', () => {}); // Ignore any parse errors from the chunk being processed
stream.unpipe(parser);
reject(error);
}
Expand Down
Binary file added spec/integration/data/invalid-xml.xlsx
Binary file not shown.
29 changes: 29 additions & 0 deletions spec/integration/workbook-xlsx-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,35 @@ describe('WorkbookReader', () => {
});
});

describe('with a spreadsheet that has an XML parse error in a worksheet', function () {
it('should reject the promise with the sax error', function () {
let unhandledRejection;
function unhandledRejectionHandler(err) {
unhandledRejection = err;
}
process.on('unhandledRejection', unhandledRejectionHandler);
const workbook = new Excel.Workbook();
return workbook.xlsx
.readFile('./spec/integration/data/invalid-xml.xlsx')
.then(
() => {
throw new Error('Promise unexpectedly fulfilled');
},
err => {
expect(err.message).to.equal('Text data outside of root node.\nLine: 1\nColumn: 1\nChar: f');
// Wait a tick before checking for an unhandled rejection
return new Promise(setImmediate);
}
)
.then(() => {
expect(unhandledRejection).to.be.undefined;
})
.finally(() => {
process.removeListener('unhandledRejection', unhandledRejectionHandler);
});
});
});

describe('with a spreadsheet that contains images', () => {
before(function() {
const testContext = this;
Expand Down

0 comments on commit 26a7019

Please sign in to comment.