diff --git a/lib/xlsx/xlsx.js b/lib/xlsx/xlsx.js index 4f6bc02c5..268d1ddb3 100644 --- a/lib/xlsx/xlsx.js +++ b/lib/xlsx/xlsx.js @@ -675,6 +675,8 @@ class XLSX { this.write(stream, options).then(() => { stream.end(); + }).catch(err=>{ + reject(err); }); }); } diff --git a/spec/integration/pr/test-pr-2244.spec.js b/spec/integration/pr/test-pr-2244.spec.js new file mode 100644 index 000000000..667446c4c --- /dev/null +++ b/spec/integration/pr/test-pr-2244.spec.js @@ -0,0 +1,23 @@ +const ExcelJS = verquire('exceljs'); + +describe('pull request 2244', () => { + it('pull request 2244- Fix xlsx.writeFile() not catching error when error occurs', async () => { + async function test() { + const workbook = new ExcelJS.Workbook(); + const worksheet = workbook.addWorksheet('sheet'); + const imageId1 = workbook.addImage({ + filename: 'path/to/image.jpg', // Non-existent file + extension: 'jpeg', + }); + worksheet.addImage(imageId1, 'B2:D6'); + await workbook.xlsx.writeFile('test.xlsx'); + } + let error; + try { + await test(); + } catch (err) { + error = err; + } + expect(error).to.be.an('error'); + }); +});