|
1 | 1 | import os |
2 | 2 | import logging |
| 3 | +import socket |
3 | 4 | import time |
4 | 5 | import traceback |
5 | 6 | import threading |
6 | | -from typing import Any, Dict, Optional |
| 7 | +from typing import Any, Dict, List, Optional |
7 | 8 |
|
8 | 9 | import octowebsocket |
9 | 10 | import requests |
@@ -328,30 +329,42 @@ def _HandleCantCreateThreadException(logger:logging.Logger, e:Exception) -> bool |
328 | 329 | # We don't want to report these to sentry, as they are common and not actionable. |
329 | 330 | @staticmethod |
330 | 331 | def IsCommonConnectionException(e:Exception) -> bool: |
| 332 | + def matchesException(exception:Exception, exceptionClass:Any, msgs:Optional[List[str]]=None) -> bool: |
| 333 | + if not isinstance(exception, exceptionClass): |
| 334 | + return False |
| 335 | + exceptionStr = str(exception).lower().strip() |
| 336 | + if msgs is not None: |
| 337 | + for t in msgs: |
| 338 | + if t.lower() in exceptionStr: |
| 339 | + return True |
| 340 | + return False |
| 341 | + |
331 | 342 | try: |
332 | 343 | # This means a device was at the IP, but the port isn't open. |
333 | | - if isinstance(e, ConnectionRefusedError): |
| 344 | + if matchesException(e, ConnectionRefusedError): |
334 | 345 | return True |
335 | | - if isinstance(e, ConnectionResetError): |
| 346 | + if matchesException(e, ConnectionResetError): |
336 | 347 | return True |
337 | 348 | # This means the IP doesn't route to a device. |
338 | | - if isinstance(e, OSError) and ("No route to host" in str(e) or "Network is unreachable" in str(e)): |
| 349 | + if matchesException(e, OSError, ["No route to host", "Network is unreachable", "Network unreachable", "Host is unreachable"]): |
| 350 | + return True |
| 351 | + if matchesException(e, socket.gaierror, ["Name does not resolve"]): |
339 | 352 | return True |
340 | 353 | # This means the other side never responded. |
341 | | - if isinstance(e, TimeoutError) and "Connection timed out" in str(e): |
| 354 | + if matchesException(e, TimeoutError, ["Connection timed out", "Operation timed out"]): |
342 | 355 | return True |
343 | | - if isinstance(e, octowebsocket.WebSocketTimeoutException): |
| 356 | + if matchesException(e, octowebsocket.WebSocketTimeoutException): |
344 | 357 | return True |
345 | 358 | # This just means the server closed the socket, |
346 | 359 | # or the socket connection was lost after a long delay |
347 | 360 | # or there was a DNS name resolve failure. |
348 | | - if isinstance(e, octowebsocket.WebSocketConnectionClosedException) and ("Connection to remote host was lost." in str(e) or "ping/pong timed out" in str(e) or "Name or service not known" in str(e)): |
| 361 | + if matchesException(e, octowebsocket.WebSocketConnectionClosedException, ["Connection to remote host was lost.", "ping/pong timed out", "Name or service not known"]): |
349 | 362 | return True |
350 | 363 | # Invalid host name. |
351 | | - if isinstance(e, octowebsocket.WebSocketAddressException) and "Name or service not known" in str(e): |
| 364 | + if matchesException(e, octowebsocket.WebSocketAddressException, ["Name or service not known"]): |
352 | 365 | return True |
353 | 366 | # We don't care. |
354 | | - if isinstance(e, octowebsocket.WebSocketConnectionClosedException): |
| 367 | + if matchesException(e, octowebsocket.WebSocketConnectionClosedException): |
355 | 368 | return True |
356 | 369 | except Exception: |
357 | 370 | pass |
|
0 commit comments