From dd8681e3a24882a94e7fc9ab0c35cbc25aac988e Mon Sep 17 00:00:00 2001 From: dan-s1 Date: Mon, 30 Sep 2024 22:42:18 +0000 Subject: [PATCH] NIFI-13819 Provided a row number and sheet name if ExcelReader throws exception when it fails to convert a value. This closes #9327 Signed-off-by: Mike Thomsen --- .../apache/nifi/excel/ExcelRecordReader.java | 10 ++++++++-- .../nifi/excel/TestExcelRecordReader.java | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/main/java/org/apache/nifi/excel/ExcelRecordReader.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/main/java/org/apache/nifi/excel/ExcelRecordReader.java index f9df73a38c2b..8c49603c1188 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/main/java/org/apache/nifi/excel/ExcelRecordReader.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/main/java/org/apache/nifi/excel/ExcelRecordReader.java @@ -76,14 +76,20 @@ public ExcelRecordReader(ExcelRecordReaderConfiguration configuration, InputStre @Override public Record nextRecord(boolean coerceTypes, boolean dropUnknownFields) throws MalformedRecordException { + Row currentRow = null; try { if (rowIterator.hasNext()) { - Row currentRow = rowIterator.next(); + currentRow = rowIterator.next(); Map currentRowValues = getCurrentRowValues(currentRow, coerceTypes, dropUnknownFields); return new MapRecord(schema, currentRowValues); } } catch (Exception e) { - throw new MalformedRecordException("Read next Record from Excel XLSX failed", e); + String exceptionMessage = "Read next Record from Excel XLSX failed"; + if (currentRow != null) { + exceptionMessage = String.format("%s on row %s in sheet %s", + exceptionMessage, currentRow.getRowNum(), currentRow.getSheet().getSheetName()); + } + throw new MalformedRecordException(exceptionMessage, e); } return null; } diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java index 3f30b53e82d1..acc0b8d2a08f 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java @@ -284,6 +284,26 @@ public void testSelectAllSheets() throws MalformedRecordException { assertEquals(7, records.size()); } + @Test + void testWhereCellValueDoesNotMatchSchemaType() { + RecordSchema schema = new SimpleRecordSchema(Arrays.asList(new RecordField("first", RecordFieldType.STRING.getDataType()), + new RecordField("second", RecordFieldType.FLOAT.getDataType()))); + List requiredSheets = Collections.singletonList("TestSheetA"); + ExcelRecordReaderConfiguration configuration = new ExcelRecordReaderConfiguration.Builder() + .withSchema(schema) + .withFirstRow(2) + .withRequiredSheets(requiredSheets) + .build(); + + final MalformedRecordException mre = assertThrows(MalformedRecordException.class, () -> { + ExcelRecordReader recordReader = new ExcelRecordReader(configuration, getInputStream(MULTI_SHEET_FILE), logger); + getRecords(recordReader, true, false); + }); + + assertInstanceOf(NumberFormatException.class, mre.getCause()); + assertTrue(mre.getMessage().contains("on row") && mre.getMessage().contains("in sheet")); + } + @Test void testPasswordProtected() throws Exception { RecordSchema schema = getPasswordProtectedSchema();