Skip to content

Prevent from unhandled promise rejection durning workbook load #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/utils/stream-buf.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ utils.inherits(StreamBuf, Stream.Duplex, {
chunk = new StringBufChunk(data);
} else if (data instanceof Buffer) {
chunk = new BufferChunk(data);
} else {
// assume string
} else if (typeof data === 'string' || data instanceof String) {
chunk = new StringChunk(data, encoding);
} else {
throw new Error('Chunk must be one of type String, Buffer or StringBuf.');
}

// now, do something with the chunk
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/zip-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ class ZipReader extends events.EventEmitter {

// ==========================================================================
// Stream.Writable interface
write(data, encoding, callback) {
async write(data, encoding, callback) {
if (this.error) {
if (callback) {
callback(this.error);
}
throw this.error;
} else {
return this.stream.write(data, encoding, callback);
try {
const result = await this.stream.write(data, encoding, callback);
return result;
} catch (err) {
this.emit('error', err);
return err;
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions spec/integration/workbook/workbook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,17 @@ describe('Workbook', () => {
expect(success).to.equal(2);
});
});
it('throw an error for wrong data type', async () => {
const wb = new ExcelJS.Workbook();
try {
await wb.xlsx.load({});
expect.fail('should fail for given argument');
} catch(e) {
expect(e.message).to.equal(
'Chunk must be one of type String, Buffer or StringBuf.'
);
}
});

describe('Sheet Views', () => {
it('frozen panes', () => {
Expand Down
11 changes: 11 additions & 0 deletions spec/unit/utils/stream-buf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ describe('StreamBuf', () => {
sb.on('error', reject);
s.pipe(sb);
}));
it('handle unsupported type of chunk', async () => {
const stream = new StreamBuf();
try {
await stream.write({});
expect.fail('should fail for given argument');
} catch (e) {
expect(e.message).to.equal(
'Chunk must be one of type String, Buffer or StringBuf.'
);
}
});
});