Skip to content

Commit

Permalink
NIFI-13819 Provided a row number and sheet name if ExcelReader throws…
Browse files Browse the repository at this point in the history
… exception when it fails to convert a value.

This closes apache#9327

Signed-off-by: Mike Thomsen <[email protected]>
  • Loading branch information
dan-s1 authored and MikeThomsen committed Oct 1, 2024
1 parent 74091e1 commit dd8681e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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();
Expand Down

0 comments on commit dd8681e

Please sign in to comment.