louischatriot/node-redis-pubsub

Node crashes on "Unexpected token"

Closed this issue · 5 comments

Hello,

I'm getting a strange error when subscribing to a channel. My script randomly crashes when receiving a message. I can't even log the message because it crashes before calling my handler. This does not happen everytime, but randomly on a message I couldn't identify yet. The crash report is :

events.js:68
        throw arguments[1]; // Unhandled 'error' event
                       ^
SyntaxError: Unexpected token #
    at Object.parse (native)
    at RedisClient.NodeRedisPubsub.on ({dir}\node_modules\node-redis-pubsub\lib\node-redis-pubsub.js:37:20)
    at RedisClient.EventEmitter.emit (events.js:123:20)
    at RedisClient.return_reply ({dir}\node_modules\node-redis-pubsub\node_modules\redis\index.js:641:22)
    at ReplyParser.RedisClient.init_parser ({dir}\node_modules\node-redis-pubsub\node_modules\redis\index.js:282:14)
    at ReplyParser.EventEmitter.emit (events.js:93:17)
    at ReplyParser.send_reply ({dir}\node_modules\node-redis-pubsub\node_modules\redis\lib\parser\javascript.js:300:10)
    at ReplyParser.execute ({dir}\node_modules\node-redis-pubsub\node_modules\redis\lib\parser\javascript.js:211:22)
    at RedisClient.on_data ({dir}\node_modules\node-redis-pubsub\node_modules\redis\index.js:504:27)
    at Socket.<anonymous> ({dir}\node_modules\node-redis-pubsub\node_modules\redis\index.js:82:14)

It indicates unexpected token #, but many of my messages contains a # . Sometimes it crashes right at the first message, sometimes it handles several messages before crashing. Any idea ?

Ok, I got it. Some message are emitted via a JAVA server, and they are badly JSON encoded

Is there any way to prevent node-redis-pubsub from crashing on this error ?

Yes there is. The call to JSON.parse in the on function should be surrounded by a try/catch block (just JSON.parse, not handler(JSON.parse)). Then on an error, an 'error' event should be emitted by the NPR object with the exception given.

I've got no time to do it but I will review a PR if you make one !

Ok, thanks, I will be happy to contribute ! However the problem is not on my "hot list" now, just on the "todo later" list ;-)

Any update on the issue?