adaltas / node-csv-parse

CSV parsing implementing the Node.js `stream.Transform` API
https://csv.js.org/parse/
804 stars 166 forks source link

Return error #320

Open omgcarry opened 3 years ago

omgcarry commented 3 years ago

Hello. I'm trying to use parse

` const parse = require('csv-parse/lib/sync')

  const records = parse(event.target.result, {
    columns: true,
    skip_empty_lines: true
  })`

In general, it works fine. But if my file contains an issue with quotes I cannot handle this error Invalid Opening Quote: a quote is found inside a field at line 2 Is there a way how to handle this error? Need to show it in the custom

element

wdavidw commented 3 years ago

Don't raise your hope too high but chances are that the relax will help you. It does not yet have its own documentation page, help is welcome, but I encourage you to look at its tests.

omgcarry commented 3 years ago

I tried relax, but it didn't help :(

metravonrech commented 3 years ago

trim helped me

FossPrime commented 3 years ago

I also had this issue... while attempting to use the sync API in a native ES Modules package. Made my own Async API by promising the callback one. Similar to the csvtojson package's Async API.

const csvParse = (data, options) => new Promise((resolve, reject) => {
  csv.parse(data, options, (err, output) => err ? reject(err) : resolve(output))
})

// Usage:
async function main () {
  const result = await csvParse('name,email\nElon,em@x.com', { columns: true })
  console.log(result) 
}
main()

It's convenient and errors bubble up correctly. The whole point of async / await is to make async code look synchronous.

That said, not bubbling errors with the Sync API is definitely a bug.

Here's a sandbox with various scenarios involving CSV and ESM https://runkit.com/hesygolu/60cab0ebed19ef001a500480

wdavidw commented 3 years ago

@rayfoss what you are proposing is not relevant to this issue. Worst, the code is not good. Just use csv-parse/lib/sync instead of using a useless promise since you don't need streaming. (and remove you comment, it creates confusion to users reading this issue)