Naltox / telegram-node-bot

Node module for creating Telegram bots.
MIT License
721 stars 143 forks source link

bot stops responding: "Error: SyntaxError: Unexpected token < in JSON at position 0" #90

Closed dutu closed 8 years ago

dutu commented 8 years ago

Bot stops responding and doesn't recover after error below is logged:

[error]
api request error: Body: undefined 
Error: SyntaxError: Unexpected token < in JSON at position 0 
    at Object.parse (native) 
    at IncomingMessage.response.on.e (/app/node_modules/tiny_request/lib/network.js:68:38) 
    at emitNone (events.js:91:20) 
    at IncomingMessage.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:934:12) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9)

Restart of entire application is required to restore the functionality

alxrm commented 8 years ago

Seem like something from Telegram.

What do you invoke before this appears?

vlad-s commented 8 years ago

That error happens whenever JSON.parse() gets HTML content as a parameter.

λ node
> JSON.parse('<htm>test</html>')
SyntaxError: Unexpected token < in JSON at position 0
    at Object.parse (native)
    at repl:1:6
    at REPLServer.defaultEval (repl.js:272:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:441:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:224:10)
    at REPLServer.Interface._line (readline.js:566:8)

This is probably an API error w/o a try/catch block.

dutu commented 8 years ago

Hi @alxrm and @vlad-s, Thanks for your feedback. You're right. It looks that the fault is triggered by a response from telegram, when html is sent back instead of JSON & errorcode.

The fault seems to be due to incomplete error handling in TelegramAPI.js at https://github.com/Naltox/telegram-node-bot/blob/master/lib/api/TelegramApi.js#L94

Incomplete error handling is also causing issue #89.

Can we please have a response from devs on if/when can this be resolved, else I'd need to migrate to another framework (cannot afford bot to stop unexpectedly)

Thank you!

Orel2888 commented 8 years ago

Same problem

Naltox commented 8 years ago

Just updated module, think that will help