From b3befc909518c8da16f9feb15c9d55b1ae915c11 Mon Sep 17 00:00:00 2001 From: Edmondo Porcu Date: Fri, 10 Nov 2023 23:22:58 -0800 Subject: [PATCH] Returning meaningful error on missing worksheet (#231) * Returning meaningful error on missing worksheet --- packages/xlsx-import/src/Importer.ts | 4 +++- .../tests/integration/vertical-lists.test.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/xlsx-import/src/Importer.ts b/packages/xlsx-import/src/Importer.ts index ed1f296..b759f93 100644 --- a/packages/xlsx-import/src/Importer.ts +++ b/packages/xlsx-import/src/Importer.ts @@ -12,7 +12,9 @@ export class Importer implements IImporter { const { worksheet } = config; const type = (config.type as ImportType) || IMPORT_TYPE_DEFAULT; const ws = this.wb.getWorksheet(worksheet); - + if (!ws) { + throw new Error(`Worksheet "${worksheet}" not found in workbook.`); + } return getStrategyByType(type)(config, ws); } diff --git a/packages/xlsx-import/tests/integration/vertical-lists.test.ts b/packages/xlsx-import/tests/integration/vertical-lists.test.ts index f1d5aa1..d39ac96 100644 --- a/packages/xlsx-import/tests/integration/vertical-lists.test.ts +++ b/packages/xlsx-import/tests/integration/vertical-lists.test.ts @@ -26,6 +26,9 @@ describe('testing vertical list - on file "marsjanie-db"', () => { ], rowOffset: 1, }, + not_existing_wroksheet: { + worksheet: 'dummy' + } }; it('worksheet "szit1"', async () => { @@ -180,4 +183,11 @@ describe('testing vertical list - on file "marsjanie-db"', () => { chai.expect(result).eql(expected); }); + + it(`should return a meaningful error when the worksheet is not found`, async() => { + const factory = new ImporterFactory(); + const importer = await factory.from('tests/data/marsjanie-db.xlsx'); + const result = () => importer.getAllItems(configs.not_existing_wroksheet); + chai.expect(result).to.throw('Worksheet "dummy" not found in workbook.'); + }); });