0

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?

Terry Sposato
  • 572
  • 2
  • 7
  • You need to show the code which catches that exception and returns it to the front end. – tgdavies Nov 30 '20 at 02:12
  • @tgdavies - I've updated the question with the try/catch where I call parseAddress() – Terry Sposato Nov 30 '20 at 02:35
  • What do you mean by "the error you get in the frontend"? How are you seeing that error? What does the rest of parse address do? – tgdavies Nov 30 '20 at 03:27
  • There is a JS frontend. For simplicity, the console log sometimes has the java.lang.IllegalArgumentException string in the error output. – Terry Sposato Nov 30 '20 at 03:59
  • What's the code which renders a response when there is an uncaught exception in your controller, and what is the code in the frontend which logs to the console? – tgdavies Nov 30 '20 at 21:21
  • The frontend is irrelevant for this question as it just displays the message that the backend is sending, in this case sometimes it displayed the message string I've defined and sometimes it adds in the exception name as well as the message string. Nowhere else in the controller is there an IllegalArgumentException. There are IOExceptions and DataAccessExceptions but both have different error message. The code snippet in the question is basically the full method besides validating the user. – Terry Sposato Nov 30 '20 at 22:31

0 Answers0