node icon indicating copy to clipboard operation
node copied to clipboard

SyntaxError: Unexpected token ';', ";{

Open mojidev7 opened this issue 1 year ago • 5 comments

Looks like something is wrong here:

SyntaxError: Unexpected token ';', ";{
  "ip":"... is not valid JSON
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\Users\Administrator\Desktop\data-analysis-co\node_modules\node-ipinfo\dist\src\ipinfoWrapper.js:109:67)
    at IncomingMessage.emit (node:events:519:28)
    at emitCloseNT (node:internal/streams/destroy:147:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21)

this error came from this line, maybe malformed response.

mojidev7 avatar Jul 25 '24 13:07 mojidev7

that's a very simple case:

Example:

function foo() {
    console.log('Hi');
    setTimeout(() => {
        console.log('lets throwing an exception.. :}}')
        throw new Error('An Amazing Error!');
    }, 2000)
}

try {
    foo()
} catch (error) {
    console.log('Error Catched!: ' + error);
}

Output:

Hi
lets throwing an exception.. :}}
/Users/mojtaba/personalp/test.js:5
        throw new Error('An Amazing Error!');
        ^

Error: An Amazing Error!
    at Timeout._onTimeout (/Users/mojtaba/personalp/test.js:5:15)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

Node.js v20.15.1

We can see that Error Catched! is not here..

mojidev7 avatar Jul 25 '24 13:07 mojidev7

Hi @mojidev7 Which version of node-ipinfo are you using?

If it's < 3.1.1 then the issue is that they don't have a fix for this issue which was added in July 2022.

Let me know if the error persists after the update.

abdullahdevrel avatar Aug 26 '24 09:08 abdullahdevrel

I this still happening (very rarely) in 3.5.3

pz129 avatar Oct 03 '24 23:10 pz129

We will look into this as soon as possible.

CC: @max-ipinfo

abdullahdevrel avatar Oct 04 '24 03:10 abdullahdevrel

@pz129 We are investigating the issues, and are currently unsure how to replicate the scenario. Can you please reach out to us through our support desk ([email protected])? We would appreciate more information from you. Thanks!

Best Regards, Abdullah (DevRel @ IPinfo.io)

abdullahdevrel avatar Oct 07 '24 12:10 abdullahdevrel

After a year, this issue still hasn't been fixed. My server just crashed in production again—the second time this year. Adding a simple try/catch around JSON.parse(xxx) would resolve it. Check my solution here: https://github.com/ipinfo/node/issues/88

sayhicoelho avatar Oct 28 '24 19:10 sayhicoelho

@sayhicoelho sorry for the wait. I've had to focus on other things unfortunately.

I'll do my best to take a look today. Thank you for your understanding.

max-ipinfo avatar Oct 28 '24 19:10 max-ipinfo

@sayhicoelho sorry for the wait. I've had to focus on other things unfortunately.

I'll do my best to take a look today. Thank you for your understanding.

Hi @max-ipinfo , any update about it ? Or at least how to catch it on our side to prevent crash on production for now? Thank you 🙏

vguillot avatar Nov 05 '24 10:11 vguillot

@vguillot @sayhicoelho as a quick update, I've been investigating this bogus JSON response and we do not have a clear reproduction case on our end.

That said, I'm definitely open to adding try/catch blocks to prevent transitive failures on your end.

max-ipinfo avatar Nov 06 '24 19:11 max-ipinfo

@max-ipinfo It isn't possible to try/catch on our end because the error occurs in an event listener.

See my example here.

sayhicoelho avatar Nov 06 '24 22:11 sayhicoelho

@max-ipinfo It isn't possible to try/catch on our end because the error occurs in a event listener.

See my example here.

@sayhicoelho sorry if I wasn't clear in my response. I meant adding try/catch around the JSON.parse calls throughout our library, calling the reject callback in case of an error.

max-ipinfo avatar Nov 06 '24 22:11 max-ipinfo

@mojidev7 I just pushed a fix to https://www.npmjs.com/package/node-ipinfo/v/3.5.5

Thank you for your patience!

max-ipinfo avatar Nov 08 '24 04:11 max-ipinfo

@sayhicoelho we've just released v4 of the library, replacing use of http(s) with node-fetch. The use of node-fetch means we're no longer directly using event listeners that can escape error catching, so wrapping lookupIp in try/catch will catch errors. We also think that the use of http(s) was the cause of the underlying error (a ; being prepend to the response when reading from the stream) so the error itself should stop.

Please update to v4 at your convenience and let us know if you encounter any further issues.

shrink avatar Nov 21 '24 06:11 shrink

@max-ipinfo @abdullahdevrel @shrink Good job thanks If faced again will inform you

mojidev7 avatar Nov 23 '24 16:11 mojidev7