apricity apricity - 9 days ago 4x
Node.js Question

Viewing piped data inside pipe error

In the following code block how can I log the piped data causing JSONStream.parse to fail?

.on('error', () => {
// here I'd like to see the data causing JOSONStream.parse to blow up
reject("Error parsing the json!");

I ask because currently when it blows up I get this sort of error message but no context to what the Invalid UTF-8 character is:

Error: Invalid JSON (Invalid UTF-8 character at position 2397 in state STRING1)
at Parser.proto.write (/var/www/data-site/pop-service/node_modules/JSONStream/node_modules/jsonparse/jsonparse.js:120:31)
at Stream.<anonymous> (/var/www/data-site/pop-service/node_modules/JSONStream/index.js:23:12)
at Stream.stream.write (/var/www/data-site/pop-service/node_modules/JSONStream/node_modules/through/index.js:26:11)
at IncomingMessage.ondata (_stream_readable.js:536:20)
at emitOne (events.js:82:20)
at IncomingMessage.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at IncomingMessage.Readable.push (_stream_readable.js:111:10)
at HTTPParser.parserOnBody (_http_common.js:124:22)
at TLSSocket.socketOnData (_http_client.js:320:20)

something like
.on('error', (data) => {...
doesn't seem to work

Solution using answer from @drinchev

//save the last chunk so that we can log it in case of parsing error
let lastRetrievedChunk = '';
data.on('data', (dd: any) => {
lastRetrievedChunk = dd.toString();

let jsonParser = JSONStream.parse('*');
jsonParser.on('error', (err: any) => {
reject(err.stack + ' lastchunk = ' + lastRetrievedChunk);


It does not work indeed, since it is event-driven process.

You can try to store the data in a variable and pass it to the error

var someString = '*';

    .on('error', () => {
       console.log( someString );
       reject("Error parsing the json!");

At any point you can also use some library like through2 which can help you to make a stream that logs the piped data.