Skip to content

Commit

Permalink
Fix some issues with transform error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 29, 2018
1 parent 4500d3b commit 0cad401
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
19 changes: 11 additions & 8 deletions lib/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class JSON2CSVTransform extends Transform {
transform.pushLine(JSON.parse(line));
} catch(e) {
if (i !== lines.length - 1) {
throw e;
e.message = 'Invalid JSON (' + line + ')'
transform.emit('error', e);
}
}
});
Expand All @@ -79,12 +80,14 @@ class JSON2CSVTransform extends Transform {

this.parser.onToken = function (token, value) {
transform.parser._onToken(token, value);

if (this.stack.length === 0
&& !transform.params.fields
&& this.mode !== Parser.C.ARRAY
&& this.mode !== Parser.C.OBJECT) {
this.onError(new Error('params should include "fields" and/or non-empty "data" array of objects'));
}
if (this.stack.length === 1) {
if (!transform.params.fields
&& this.mode !== Parser.C.ARRAY
&& this.mode !== Parser.C.OBJECT) {
throw new Error('params should include "fields" and/or non-empty "data" array of objects');
}
if(this.depthToEmit === undefined) {
// If Array emit its content, else emit itself
this.depthToEmit = (this.mode === Parser.C.ARRAY) ? 1 : 0;
Expand All @@ -98,10 +101,10 @@ class JSON2CSVTransform extends Transform {
}

this.parser.onError = function (err) {
if(err.message.indexOf('at position') > -1) {
if(err.message.indexOf('Unexpected') > -1) {
err.message = 'Invalid JSON (' + err.message + ')';
}
transform.emit('error', err)
transform.emit('error', err);
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/json/defaultInvalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{ "carModel": "Audi", "price": 0, "color": "blue" },
{ "carModel": "BMW", "price": 15000, "color": "red", "transmission": "manual" },
{ "carModel": "Mercedes", "price": 20000, "color": "yellow",
{ "carModel": "Porsche", "price": 30000, "color": "green" }
]
4 changes: 4 additions & 0 deletions test/fixtures/json/ldjsonInvalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ "carModel": "Audi", "price": 0, "color": "blue" }
{ "carModel": "BMW", "price": 15000, "color": "red", "transmission": "manual" }
{ "carModel": "Mercedes", "price": 20000, "color": "yellow"
{ "carModel": "Porsche", "price": 30000, "color": "green" }
52 changes: 49 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const Readable = require('stream').Readable
const test = require('tape');
const Json2csvParser = require('../lib/json2csv').Parser;
const Json2csvTransform = require('../lib/json2csv').Transform;
const json2csv = require('../lib/json2csv');
const Json2csvParser = json2csv.Parser;
const Json2csvTransform = json2csv.Transform;
const parseLdJson = require('../lib/parse-ldjson');
const loadFixtures = require('./helpers/load-fixtures');

Expand All @@ -16,6 +17,14 @@ Promise.all([
const jsonFixturesStreams = fixtures[1];
const csvFixtures = fixtures[2];

test('should parse json to csv and infer the fields automatically using parse method', (t) => {
const csv = json2csv.parse(jsonFixtures.default);

t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.default);
t.end();
});

test('should error if input data is not an object', (t) => {
const input = 'not an object';
try {
Expand Down Expand Up @@ -642,15 +651,52 @@ Promise.all([
.on('error', err => t.notOk(err));
});

test('should error on invalid ld-json input data', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission'],
ldjson: true
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixturesStreams.ldjsonInvalid().pipe(transform);

processor.on('finish', () => {
t.notOk(true);
t.end();
});
processor.on('error', (error) => {
t.ok(error.message.indexOf('Invalid JSON') !== -1);
t.end();
});
});

test('should error if input data is not an object', (t) => {
const input = new Readable();
input._read = () => {};
input.push('not an object');
input.push('"not an object"');
input.push(null);

const transform = new Json2csvTransform();
const processor = input.pipe(transform);

processor.on('finish', () => {
t.notOk(true);
t.end();
});
processor.on('error', (error) => {
t.equal(error.message, 'params should include "fields" and/or non-empty "data" array of objects');
t.end();
});
});

test('should error on invalid json input data', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission']
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixturesStreams.defaultInvalid().pipe(transform);

processor.on('finish', () => {
t.notOk(true);
t.end();
Expand Down

0 comments on commit 0cad401

Please sign in to comment.