Browse Source

properly differentiate between timeout (printer not reachable) and api error

Thomas Buck 3 years ago
parent
commit
df1fe1cece
1 changed files with 9 additions and 7 deletions
  1. 9
    7
      src/octotray.py

+ 9
- 7
src/octotray.py View File

461
         else:
461
         else:
462
             data = content.encode('ascii')
462
             data = content.encode('ascii')
463
             request = urllib.request.Request(url, data, headers)
463
             request = urllib.request.Request(url, data, headers)
464
+
464
         try:
465
         try:
465
             with urllib.request.urlopen(request, None, self.networkTimeout) as response:
466
             with urllib.request.urlopen(request, None, self.networkTimeout) as response:
466
                 text = response.read()
467
                 text = response.read()
467
                 return text
468
                 return text
468
-        except urllib.error.URLError:
469
-            pass
470
-        except urllib.error.HTTPError:
471
-            pass
472
-        return "error"
469
+        except (urllib.error.URLError, urllib.error.HTTPError) as error:
470
+            print("Error requesting URL \"" + url + "\": \"" + str(error) + "\"")
471
+            return "error"
472
+        except socket.timeout:
473
+            print("Timeout waiting for response to \"" + url + "\"")
474
+            return "timeout"
473
 
475
 
474
     def sendPostRequest(self, host, key, path, content):
476
     def sendPostRequest(self, host, key, path, content):
475
         headers = {
477
         headers = {
560
 
562
 
561
     def getMethod(self, host, key):
563
     def getMethod(self, host, key):
562
         r = self.sendGetRequest(host, key, "plugin/psucontrol")
564
         r = self.sendGetRequest(host, key, "plugin/psucontrol")
563
-        if r == "error":
565
+        if r == "timeout":
564
             return "unknown"
566
             return "unknown"
565
 
567
 
566
         try:
568
         try:
571
             pass
573
             pass
572
 
574
 
573
         r = self.sendGetRequest(host, key, "system/commands/custom")
575
         r = self.sendGetRequest(host, key, "system/commands/custom")
574
-        if r == "error":
576
+        if r == "timeout":
575
             return "unknown"
577
             return "unknown"
576
 
578
 
577
         try:
579
         try:

Loading…
Cancel
Save