Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Return error #320

Open
omgcarry opened this issue Jun 10, 2021 · 5 comments
Open

Return error #320

omgcarry opened this issue Jun 10, 2021 · 5 comments
Labels

Comments

@omgcarry
Copy link

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

@omgcarry omgcarry added the bug label Jun 10, 2021
@wdavidw
Copy link
Member

wdavidw commented Jun 10, 2021

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
Copy link
Author

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

@metravonrech
Copy link

trim helped me

@FossPrime
Copy link

FossPrime commented Jun 16, 2021

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,[email protected]', { 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
Copy link
Member

wdavidw commented Jun 16, 2021

@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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants