mayur mohan created CAMEL-24522:
-----------------------------------

             Summary: camel-csv: wrap CsvUnmarshaller IOException with 
descriptive message for malformed quoted fields
                 Key: CAMEL-24522
                 URL: https://issues.apache.org/jira/browse/CAMEL-24522
             Project: Camel
          Issue Type: Improvement
          Components: camel-csv
            Reporter: mayur mohan


h2. Problem

When Apache Commons CSV fails to parse a malformed CSV file (e.g. extra 
characters after a closing quote before the delimiter), it throws a raw 
IOException with an unhelpful message:

{code:java}
java.io.IOException: (line 1) invalid char between encapsulated token and 
delimiter
    at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:281)
    at org.apache.commons.csv.Lexer.nextToken(Lexer.java:158)
    at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:674)
    at org.apache.commons.csv.CSVParser.createHeaders(CSVParser.java:483)
    at org.apache.commons.csv.CSVParser.<init>(CSVParser.java:412)
    at org.apache.commons.csv.CSVParser.<init>(CSVParser.java:378)
    at 
org.apache.camel.dataformat.csv.CsvUnmarshaller$BulkCsvUnmarshaller.unmarshal(CsvUnmarshaller.java:105)
    at 
org.apache.camel.dataformat.csv.CsvDataFormat.unmarshal(CsvDataFormat.java:95)
    at 
com.sap.it.rt.csvtoxml.converter.internal.CsvToJavaListConverter.unmarshalCsv(CsvToJavaListConverter.java:57)
    at 
com.sap.it.rt.csvtoxml.converter.CsvToXmlProcessor.process(CsvToXmlProcessor.java:79)
{code}

The term "encapsulated token" is internal Commons CSV terminology that 
operators do not understand. When they see this error they do not know what is 
wrong with their CSV or how to fix it.

h2. Root Cause

The {{CsvUnmarshaller}} (both {{BulkCsvUnmarshaller}} and 
{{StreamCsvUnmarshaller}}) does not catch the {{IOException}} thrown by 
{{CSVParser}} construction or iteration, so the raw Commons CSV message 
propagates unchanged.

A common real-world trigger is a CSV header line like:

{code}
"OrderId"x,"Name"
{code}

where a quoted field has extra characters after the closing quote and before 
the delimiter.

h2. Fix

Catch {{IOException}} in both unmarshal paths and re-throw with a message that:
# Prefixes with {{CSV parse failed:}} to identify it as a Camel-level error
# Explains what "encapsulated token" means in plain language
# Gives a concrete example of the malformed input pattern
# Tells the operator what to check (delimiter, quote character, escaped quotes)

h3. After the fix the error reads:

{code}
CSV parse failed: (line 1) invalid char between encapsulated token and 
delimiter.
A quoted field has extra characters after the closing quote and before the 
delimiter
(example: "abc"x,def). Check delimiter, quote character, and escaped quotes 
("").
{code}

h2. Affected file

{{components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvUnmarshaller.java}}

Both inner classes: {{BulkCsvUnmarshaller}} and {{StreamCsvUnmarshaller}}.

h2. Impact

Error message only -- no behaviour change, no new dependencies, no API changes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to