You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
When I wrote to the tcp server with some string not a JSON format, it didn't respond me an 'Parse Error', but the server crashed with 'Error: Unexpected "a" at position 0 in state START'.
The error was thrown from jsonparse module, but unhandled in Utils.parseStream
I fix this with the fellowing code, more details in the comment.
Utils.parseStream = function(stream, options, onRequest) {
var result = JSONStream.parse();
stream.pipe(result);
result.on('data', function(data) {
// apply reviver walk function to prevent stringify/parse again
if(typeof(options.reviver) === 'function') {
data = Utils.walk({'': data}, '', options.reviver);
}
onRequest(null, data);
});
// the jsonparse module always parse until buffer end,
// because the thrown error has passed to JSONStream stream emit,
// it never stop while parsing error.
// e.g.
// if buffer is 'abc', it will emit error three times:
// Unexpected "a" at position 0 in state START
// Unexpected "b" at position 1 in state START
// Unexpected "c" at position 2 in state START
//
// add a errored flag to prevent onRequest error from calling more than once
var errored = false;
result.on('error', function(err) {
if (!errored) {
onRequest(err);
errored = true;
}
});
};
I'm not sure it is graceful, so I just submit a issue not pr. Hope you fix it in a better way.
The text was updated successfully, but these errors were encountered:
Hi,
When I wrote to the tcp server with some string not a JSON format, it didn't respond me an 'Parse Error', but the server crashed with 'Error: Unexpected "a" at position 0 in state START'.
The error was thrown from
jsonparse
module, but unhandled inUtils.parseStream
I fix this with the fellowing code, more details in the comment.
I'm not sure it is graceful, so I just submit a issue not pr. Hope you fix it in a better way.
The text was updated successfully, but these errors were encountered: