Skip to content

Commit c206f92

Browse files
committed
Fixing more minor bugs
1 parent 3ff4018 commit c206f92

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"fsensor",
9292
"fstring",
9393
"FULLDICT",
94+
"gaierror",
9495
"gcode",
9596
"geteuid",
9697
"getpwnam",

moonraker_octoeverywhere/moonrakercredentialmanager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def TryToGetOneshotToken(self, apiKey:Optional[str]=None) -> Optional[str]:
6363
if result is None:
6464
raise Exception("Failed to get the oneshot token from moonraker.")
6565
if result.StatusCode != 200:
66+
if result.StatusCode == 401:
67+
self.Logger.info("Failed to get the oneshot token from moonraker. Unauthorized. The API key is likely invalid.")
68+
return None
6669
raise Exception("Failed to get the oneshot token from moonraker. "+str(result.StatusCode))
6770

6871
# Read the response.

moonraker_octoeverywhere/moonrakerwebcamhelper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ def _ValidateAndFixupWebCamSettings(self, webcamSettings:WebcamSettingItem) -> b
610610
if webcamSettings.SnapshotUrl is None or len(webcamSettings.SnapshotUrl) == 0:
611611
webcamSettings.SnapshotUrl = self._TryToFigureOutSnapshotUrl(webcamSettings.StreamUrl)
612612

613+
# We have seen the rotation be a string in some cases, so ensure it's an int.
614+
if webcamSettings.Rotation is None or isinstance(webcamSettings.Rotation, str):
615+
self.Logger.warning("Webcam helper found an invalid rotation, resetting to 0. Value: '"+str(webcamSettings.Rotation)+"'")
616+
webcamSettings.Rotation = 0
617+
613618
# Ensure these are the correct types.
614619
webcamSettings.FlipH = bool(webcamSettings.FlipH)
615620
webcamSettings.FlipV = bool(webcamSettings.FlipV)

octoeverywhere/Webcam/webcamutil.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def GetSnapshotFromStream(logger:logging.Logger, result:HttpResult, validateMult
9393
if value.find('--') != -1:
9494
value = value.split('--')[0].strip()
9595
if len(p) == 2:
96+
# We have seen weird cases where there's a content-length header, but it's empty, and then there's another that has a length.
97+
if len(value) == 0:
98+
continue
9699
frameSizeInt = int(value)
97100

98101
# Break when done.

octoeverywhere/notificationshandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ def GetNotificationSnapshot(self, snapshotResizeParams:Optional[SnapshotResizePa
933933
self.Logger.warning("Can't manipulate image because the Image rotation lib failed to import.")
934934
except Exception as e:
935935
# Note that in the case of an exception we don't overwrite the original snapshot buffer, so something can still be sent.
936-
if "name 'Image' is not defined" in str(e):
936+
if isinstance(e, NameError) and "Image" in str(e):
937937
self.Logger.info("Can't manipulate image because the Image rotation lib failed to import.")
938938
if "cannot identify image file" in str(e):
939939
self.Logger.info("Can't manipulate image because the Image lib can't figure out the image type.")

octoeverywhere/sentry.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22
import logging
3+
import socket
34
import time
45
import traceback
56
import threading
6-
from typing import Any, Dict, Optional
7+
from typing import Any, Dict, List, Optional
78

89
import octowebsocket
910
import requests
@@ -328,30 +329,42 @@ def _HandleCantCreateThreadException(logger:logging.Logger, e:Exception) -> bool
328329
# We don't want to report these to sentry, as they are common and not actionable.
329330
@staticmethod
330331
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+
331342
try:
332343
# This means a device was at the IP, but the port isn't open.
333-
if isinstance(e, ConnectionRefusedError):
344+
if matchesException(e, ConnectionRefusedError):
334345
return True
335-
if isinstance(e, ConnectionResetError):
346+
if matchesException(e, ConnectionResetError):
336347
return True
337348
# 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"]):
339352
return True
340353
# 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"]):
342355
return True
343-
if isinstance(e, octowebsocket.WebSocketTimeoutException):
356+
if matchesException(e, octowebsocket.WebSocketTimeoutException):
344357
return True
345358
# This just means the server closed the socket,
346359
# or the socket connection was lost after a long delay
347360
# 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"]):
349362
return True
350363
# 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"]):
352365
return True
353366
# We don't care.
354-
if isinstance(e, octowebsocket.WebSocketConnectionClosedException):
367+
if matchesException(e, octowebsocket.WebSocketConnectionClosedException):
355368
return True
356369
except Exception:
357370
pass

0 commit comments

Comments
 (0)