Particle Library for Node.js Error

I’m running the particle-api-js node package in my application and it seems that there is an uncaught error. I’ve attached the error below. The last line is output from my custom logging service (everything after the arrow is what is being given to the logging service. It is being called by particle.login(config.particle).then(self.loginSuccess).catch(self.loginError); where self.loginError just takes in the error and outputs it to the custom logging service

Has anyone else ran into this problem?

It seems like the particle cloud service is erroring out with a 502 (Bad Gateway) and this error is not a in the JSON format which errors out the JSON.parse on line 120 in the EventStream.js. I’ve looked at this line and it seems like it’s wrapped by a try catch but something is not going right with the catch.

I can give more code or better explanation if needed.

OS: CentOS 7
Node: v6.9.4
Particle: v6.4.3

SyntaxError: Unexpected token < in JSON at position 0
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/srv/transformer-monitor/server/node_modules/particle-api-js/lib/EventStream.js:120:22)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)
[04-03-2017 20:50:50][ERROR] Particle--> Log in error: Error: HTTP error 502 from https://api.particle.io/oauth/token
1 Like

Hello,

I am going to ping someone that might be able to help, @Dave are you able to help with this?

Kyle

1 Like

I’m not sure what I was thinking earlier but it looks like the particle cloud is just giving back a 502 error (Bad Gateway). The SyntaxError just being printed out to stderr but is being catch correctly.

This isn’t an issue other than the Particle cloud having a 502 (Bad Gateway) every so often.

Hey all,

Thanks for the ping! I’ll look into it.

Thanks,
David

Got something similar crashed our server last night:

undefined:1
<html>
^

SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/Users/macbook4/node_modules/particle-api-js/lib/EventStream.js:120:22)
    at emitNone (events.js:72:20)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at doNTCallback2 (node.js:441:9)
    at process._tickCallback (node.js:355:17)

I’m having a similar error occasionally with my python script (device and access tokens changed for privacy). Is there any update on this? It happens every couple weeks.

Traceback (most recent call last):
File “/Library/Python/2.7/site-packages/pyparticleio/ParticleCloud.py”, line 276, in _event_loop
for msg in sse_client:
File “/Library/Python/2.7/site-packages/sseclient.py”, line 72, in next
self._connect()
File “/Library/Python/2.7/site-packages/sseclient.py”, line 52, in _connect
self.resp.raise_for_status()
File “/Library/Python/2.7/site-packages/requests/models.py”, line 929, in raise_for_status
raise HTTPError(http_error_msg, response=self)
HTTPError: 502 Server Error: Bad Gateway for url: https://api.particle.io/v1/devices/1234567890/events/Electron?access_token=1234567890abcdefgh

1 Like

Any updates on this?

Hi @apearson,

So, generally speaking, getting an 500-series error back from a server indicates a temporary problem. This could mean that server is in the process of restarting, or a bug that’s being fixed, etc. So I would make sure your script attempts to log back in after a short delay if you hit an error like this, and / or report the error to us so we can fix it.

Can you give more details about what errors you’re seeing, or how often / when / etc?

Thanks,
David

@Dave, I’ve seen this crash a server a few times too. i think it’s hard to catch because it happens outside the scope of the app code (in particle SDK event streaming code). what is the guidance for catching errors here, short of modifying SDK?

approaches that don’t catch this event streaming error:

  • try catch around login
  • try catch around particle.getEventStream()
  • try catch around particleEventStream.on() handlers

Heya @dwcares,

Hmm, so, in theory, the SDK should be throwing errors in a way that’s catchable by the calling code. So the eventStream should have an error event that it emits, or the promises from login / other stuff should reject gracefully in an error. It’s a good idea to wrap the insides of your callbacks in such a way as to catch and handle your exceptions. What kind of code are we talking about here is taking down the server?

In terms of process, I’m happy to help file a bug where you’re seeing the crash, if you have a stack trace or example code or anything that’s always extremely welcome, or if not that’s okay too, whatever we can use to track down the bad behavior.

Thanks,
David

Thanks @Dave ! I wasn’t handling the ‘error’ event!

others (@andrewpe) , make sure you’re listening to ‘error’ handler when you call ‘getEventStream()’ because it will throw an error if the Particle cloud hiccups like the above cases

      const stream = await this._particle.getEventStream({
            deviceId: this._deviceInfo.deviceId,
            auth: this._deviceInfo.authToken
        })

        stream.on('error', (msg) => {
            console.error(`Particle event streaming error: ${msg}`)
        })
3 Likes