fdegier / homebridge-jablotron-alarm

Homebridge plugin for connecting Jablotron JA-100 alarm systems to Homekit.
The Unlicense
26 stars 6 forks source link

Better API errors handling #78

Closed grmelacz closed 2 years ago

grmelacz commented 2 years ago

Hi, recently I'm getting quite a lot invalid responses to Jablotron API queries. I suspect it might be connected with the fact Jablotron cut out Russia and they might be attacking their servers.

Anyway, some of the problems that happen quite often follow:

[24. 3. 2022 17:57:14] [Jablotron] Error: SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (/usr/lib/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:42)
    at IncomingMessage.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1346:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
[24. 3. 2022 17:57:14] [Jablotron] Unable to parse JSON data: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>

or

[24. 3. 2022 17:32:04] [Jablotron] Error: Client network socket disconnected before secure TLS connection was established
    at connResetException (node:internal/errors:691:14)
    at TLSSocket.onConnectEnd (node:_tls_wrap:1585:19)
    at TLSSocket.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1346:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ECONNRESET',
  path: null,
  host: 'api.jablonet.net',
  port: 443,
  localAddress: undefined
}

and

[24. 3. 2022 17:36:18] [Jablotron] {
  status: false,
  error_status: 'service_unavailable',
  error_message: 'The service is not available.',
  checksum: '04bc47918ae2eeef54318ff0c0dc9f52',
  server_id: '5530945-1648139778',
  client_id: null
}
[24. 3. 2022 17:36:18] [Jablotron] Error: service_unavailable
    at /usr/lib/node_modules/homebridge-jablotron/lib/jablotron-client.js:36:31
    at IncomingMessage.<anonymous> (/usr/lib/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:21)
    at IncomingMessage.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1346:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

These appear to be not handled properly. Could you please update the plugin to handle these errors too?

fdegier commented 2 years ago

Thanks for reporting! I share your hypothesis, I've seen many weird errors show up in different applications since Ukraine conflict. Tomorrow I might have some time to look at it.

CC: @misncz

tampiss commented 2 years ago

I do not know if this is anyhow related, but I am getting those errors lately.. Sometimes the Homebridge is restored, but sometimes it just crashes the Raspi and I need to restart (turn on) manually.

[3/25/2022, 9:55:26 AM] [Jablotron] Error: getaddrinfo EAI_AGAIN api.jablonet.net [3/25/2022, 9:56:26 AM] [Jablotron] Error: getaddrinfo EAI_AGAIN api.jablonet.net [3/25/2022, 9:57:26 AM] [Jablotron] Error: getaddrinfo EAI_AGAIN api.jablonet.net [3/25/2022, 9:58:26 AM] [Jablotron] Error: getaddrinfo EAI_AGAIN api.jablonet.net [3/25/2022, 9:58:32 AM] Error: send ENETUNREACH 224.0.0.251:5353 at doSend (node:dgram:722:16) at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18) at afterDns (node:dgram:668:5) at processTicksAndRejections (node:internal/process/task_queues:84:21) [3/25/2022, 9:58:32 AM] Error: send ENETUNREACH 224.0.0.251:5353 at doSend (node:dgram:722:16) at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18) at afterDns (node:dgram:668:5) at processTicksAndRejections (node:internal/process/task_queues:84:21) [3/25/2022, 9:58:32 AM] Got SIGTERM, shutting down Homebridge...

Thank you for nay help, Martin

grmelacz commented 2 years ago

I'm getting EAI_AGAIN too. I thought it was my connection/network-related.

[25. 3. 2022 10:16:33] [Jablotron] Error: getaddrinfo EAI_AGAIN api.jablonet.net
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
  errno: -3001,
  code: 'EAI_AGAIN',
  syscall: 'getaddrinfo',
  hostname: 'api.jablonet.net'
}

Another frequent error is this one:

[26. 3. 2022 4:40:07] [Jablotron] Error: Method not supported by Jablotron
    at /usr/lib/node_modules/homebridge-jablotron/lib/jablotron-client.js:33:31
    at IncomingMessage.<anonymous> (/usr/lib/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:21)
    at IncomingMessage.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1346:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
fdegier commented 2 years ago

I have followed my logs over the last couple of days and don't see any errors. EAI_AGAIN is a DNS lookup error, might be ISP related?

We can implement a catch for EAI_AGAIN but I would like to know what the cause is.

misncz commented 2 years ago

They all are very likely ISP provider related - something with DNS and/or local network

grmelacz commented 2 years ago

Yes, I thought so. Other plugins have that probably sorted out as this one is the only one reporting this error. I got only API timeouts/bad gateways from other ones.

misncz commented 2 years ago

Other plugins don’t need to call remote server like this one. That is very likely why you don’t see them. As @fdegier said we might improve handling of these but it will just mask out the issue itself

fdegier commented 2 years ago

I finally logged some errors too:

[3/30/2022, 4:40:20 AM] [Jablotron] Error: Method not supported by Jablotron
    at /homebridge/node_modules/homebridge-jablotron/lib/jablotron-client.js:33:31
    at IncomingMessage.<anonymous> (/homebridge/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:21)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
[3/30/2022, 4:40:20 AM] [Jablotron] Error: Method not supported by Jablotron
    at /homebridge/node_modules/homebridge-jablotron/lib/jablotron-client.js:33:31
    at IncomingMessage.<anonymous> (/homebridge/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:21)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
[3/30/2022, 5:08:39 AM] [Homebridge UI] Starting terminal session
[3/30/2022, 5:09:25 AM] [Homebridge UI] Terminal session ended.
[3/30/2022, 6:05:01 AM] [Jablotron] Error: SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (/homebridge/node_modules/homebridge-jablotron/lib/jablotron-client.js:68:42)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
[3/30/2022, 6:05:01 AM] [Jablotron] Unable to parse JSON data: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
misncz commented 2 years ago

We should add handling for these - they indicate issue on server (Jablotron cloud) itself and there's nothing we could do about them. We can ignore/skip them only and wait for next successfull iteration (call to the server)

misncz commented 2 years ago

Ideally we should be checking response codes and proceed further if it was 200 (OK) only

fdegier commented 2 years ago

Yes 100%, I think we can add 2 types of handlers but maybe we that overthinking it.

  1. Jablotron server error
  2. ISP error

I'll see what I can do. I first need to fix my entire Homebridge setup as it just stopped responding completely.

fdegier commented 2 years ago

Ok so I went through the reported errors, they are actually being handled, I made a diff with more explicitly noting that we are actively handling them.

The errors reported in this issue are all network related, either Jablotron, ISP or users. Very unfortunate but nothing we really can do about that, we can mask them in the plugin but I'm not comfortable with withholding that information from users.