Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

JSON syntax error is sometimes silently ignored #145

Open
ComFreek opened this issue Dec 31, 2017 · 0 comments
Open

JSON syntax error is sometimes silently ignored #145

ComFreek opened this issue Dec 31, 2017 · 0 comments

Comments

@ComFreek
Copy link

ComFreek commented Dec 31, 2017

JSONStream 1.3.2 silently ignores JSON syntax errors sometimes, for example it does so for {"], but rightfully emits an error for a{"].

Minimal working example:

const JSONStream = require('JSONStream');
const fs = require('fs');

const FILE = 'json-stream-bug.json';
fs.writeFileSync(FILE, '{"]', { encoding: 'utf8' });

fs.createReadStream(FILE, {
	encoding: 'utf8'
})
.on('error', err => {
	console.error(err);
})
.pipe(JSONStream.parse())
.on('error', err => {
	console.error(err);
})
.on('data', data => {
	console.log(data);
});

Workaround

Check if the end event gets emitted without data having been emitted before.

const JSONStream = require('JSONStream');
const fs = require('fs');

const FILE = 'json-stream-bug.json';
fs.writeFileSync(FILE, '{"]', { encoding: 'utf8' });

let dataReceivedYet = false;

fs.createReadStream(FILE, {
	encoding: 'utf8'
})
.on('error', err => {
	console.error(err);
})
.pipe(JSONStream.parse())
.on('error', err => {
	console.error(err);
})
.on('data', data => {
	dataReceivedYet = true;
	console.log(data);
})
.on('end', () => {
	if (!dataReceivedYet) {
		// Error situation
		console.error('Unknown JSON parse error');
	}
});
@ComFreek ComFreek changed the title JSON syntax error of is silently ignored JSON syntax error is sometimes silently ignored Dec 31, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant