Skip to content

csv-stringify/lib/sync should allow errors to be caught #296

@mshick

Description

@mshick

Summary

I've noticed that the csv-stringify/lib/sync throws an uncatchable stream error on bad input data. E.g., [ 1, {a: '3', b: '4'}]. Ideally it would throw an error the consuming code can handle. The type of error, when caught, looks like this:

Error: Invalid Record: expect an array or an object, got 1
    at Stringifier._transform (file:///Users/mshick/Code/takeshape/node-csv/packages/csv-stringify/lib/index.js:311:23)
    at Stringifier.Transform._read (internal/streams/transform.js:205:10)
    at Stringifier.Transform._write (internal/streams/transform.js:193:12)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at Stringifier.Writable.write (internal/streams/writable.js:303:10)

Motivation

Using csv-stringify/lib/sync in production, feeding the function potentially bad / dirty user input, would require that I can catch errors from it.

Alternative

I am currently using the async version, but as the only async fn in the entire related code path it has necessitated a lot of inefficient code.

Draft

As a naive fix I'd add an error handler to the sync code to swallow the error, e.g.,

  stringifier.on("error", (err) => {
    console.error(err);
  });

I've been working on a PR, trying to not just handle, but also throw the error to the consumer, but haven't yet found a way. The finished fn in Node 10 might be a way.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions