Skip to content

Commit 7e44178

Browse files
committed
Change to HttpError and check status in wget
1 parent 47a5819 commit 7e44178

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

adafruit_pyportal.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
CONTENT_IMAGE = const(3)
116116

117117

118+
class HttpError(Exception):
119+
"""HTTP Specific Error"""
120+
121+
118122
class Fake_Requests:
119123
"""For faking 'requests' using a local file instead of the network."""
120124

@@ -758,10 +762,25 @@ def wget(self, url, filename, *, chunk_size=12000):
758762

759763
self.neo_status((100, 100, 0))
760764
r = requests.get(url, stream=True)
765+
761766
headers = {}
762767
for title, content in r.headers.items():
763768
headers[title.lower()] = content
764769

770+
if r.status_code == 200:
771+
print("Reply is OK!")
772+
self.neo_status((0, 0, 100)) # green = got data
773+
else:
774+
if self._debug:
775+
if "content-length" in headers:
776+
print("Content-Length: {}".format(int(headers["content-length"])))
777+
if "date" in headers:
778+
print("Date: {}".format(headers["date"]))
779+
self.neo_status((100, 0, 0)) # red = http error
780+
raise HttpError(
781+
"Code {}: {}".format(r.status_code, r.reason.decode("utf-8"))
782+
)
783+
765784
if self._debug:
766785
print(headers)
767786
if "content-length" in headers:
@@ -946,8 +965,8 @@ def fetch(self, refresh_url=None, timeout=10):
946965
if "date" in headers:
947966
print("Date: {}".format(headers["date"]))
948967
self.neo_status((100, 0, 0)) # red = http error
949-
raise OSError(
950-
"HTTP {}: {}".format(r.status_code, r.reason.decode("utf-8"))
968+
raise HttpError(
969+
"Code {}: {}".format(r.status_code, r.reason.decode("utf-8"))
951970
)
952971

953972
if self._debug and content_type == CONTENT_TEXT:

0 commit comments

Comments
 (0)