I am parsing a CSV file using parallelStream() and have noticed some odd behaviour. The code in question is here:
public List<AddressRaw> parseAddress(List<CSVRecord> records) {
return records.subList(1, records.size())
.parallelStream()
.flatMap(record -> {
// There should only be 10 columns in the CSV
if (record.size() > 10) {
log.error("Too many columns were detected on row number " + record.getRecordNumber());
throw new IllegalArgumentException("Too many columns were detected on row number " + record.getRecordNumber());
}
The controller where I call the above method:
try {
// csv files are identified by either text/csv or application/vnd.ms-excel
if (file.isEmpty() || Objects.isNull(file.getContentType()) || !(file.getContentType().equals("text/csv") || file.getContentType().equals("application/vnd.ms-excel"))) {
log.error("File uploaded is not a csv");
throw new BadRequestException("File uploaded is not a csv");
}
List<CSVRecord> records = CSVHelper.recordsFromBytes(file.getBytes());
// convert the rows into a format mirroring a db transit time
List<AddressRaw> rawAddresss = addressService.parseAddress(records);
if (rawAddresss.isEmpty()) {
throw new BadRequestException("No addresses found");
}
// execute the upsert in batches (Upserts count as two changes, as the insert fails then an update occurs)
Integer addressesInserted = addressService.batchInsertAddresses(rawAddresss, targetUserId);
LocalDateTime end = LocalDateTime.now();
log.info("Upserted {} AddressRaw records in: {}ms", addressesInserted, start.until(end, ChronoUnit.MILLIS));
return ResponseEntity.ok(addressesInserted);
} catch (IOException e) {
log.error("Unable to parse csv file: ", e);
} catch (DataAccessException e) {
log.error("Unable to execute SQL: ", e);
}
Sometimes the error that I get in the frontend is "Too many columns were detected on row number 3" for example, which is what I would expect. Sometimes the error I get is "java.lang.IllegalArgumentException: Too many columns were detected on row number 3" and I'm not exactly sure why it prints off the exception name as well? Is there something I am missing here?