Skip to content

Commit f3d504b

Browse files
authored
Merge pull request #164 from dannystaple/use-connection-errors
Make these connection errors distinct from other error types.
2 parents a8c8f71 + 2cf1d45 commit f3d504b

13 files changed

+71
-71
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
3939
"""Given a hostname and a port name, return a 'socket.getaddrinfo'
4040
compatible list of tuples. Honestly, we ignore anything but host & port"""
4141
if not isinstance(port, int):
42-
raise RuntimeError("Port must be an integer")
42+
raise ValueError("Port must be an integer")
4343
ipaddr = _the_interface.get_host_by_name(host)
4444
return [(AF_INET, socktype, proto, "", (ipaddr, port))]
4545

@@ -56,7 +56,7 @@ def __init__(
5656
self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, socknum=None
5757
):
5858
if family != AF_INET:
59-
raise RuntimeError("Only AF_INET family supported")
59+
raise ValueError("Only AF_INET family supported")
6060
self._type = type
6161
self._buffer = b""
6262
self._socknum = socknum if socknum else _the_interface.get_socket()
@@ -74,7 +74,7 @@ def connect(self, address, conntype=None):
7474
if not _the_interface.socket_connect(
7575
self._socknum, host, port, conn_mode=conntype
7676
):
77-
raise RuntimeError("Failed to connect to host", host)
77+
raise ConnectionError("Failed to connect to host", host)
7878
self._buffer = b""
7979

8080
def send(self, data): # pylint: disable=no-self-use
@@ -105,7 +105,7 @@ def readline(self, eol=b"\r\n"):
105105
self._buffer += _the_interface.socket_read(self._socknum, avail)
106106
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout:
107107
self.close() # Make sure to close socket so that we don't exhaust sockets.
108-
raise RuntimeError("Didn't receive full response, failing out")
108+
raise OSError("Didn't receive full response, failing out")
109109
firstline, self._buffer = self._buffer.split(eol, 1)
110110
gc.collect()
111111
return firstline

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def connect_normal(self):
144144
self.esp.connect_AP(bytes(ssid, "utf-8"), bytes(password, "utf-8"))
145145
failure_count = 0
146146
self.pixel_status((0, 100, 0))
147-
except (ValueError, RuntimeError) as error:
147+
except OSError as error:
148148
print("Failed to connect, retrying\n", error)
149149
failure_count += 1
150150
if failure_count >= self.attempts:
@@ -173,7 +173,7 @@ def create_ap(self):
173173
self.esp.create_AP(bytes(self.ssid, "utf-8"), None)
174174
failure_count = 0
175175
self.pixel_status((0, 100, 0))
176-
except (ValueError, RuntimeError) as error:
176+
except OSError as error:
177177
print("Failed to create access point\n", error)
178178
failure_count += 1
179179
if failure_count >= self.attempts:
@@ -203,7 +203,7 @@ def connect_enterprise(self):
203203
failure_count = 0
204204
self.pixel_status((0, 100, 0))
205205
sleep(1)
206-
except (ValueError, RuntimeError) as error:
206+
except OSError as error:
207207
print("Failed to connect, retrying\n", error)
208208
failure_count += 1
209209
if failure_count >= self.attempts:

adafruit_esp32spi/digitalio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def init(self, mode=IN):
6161
self._mode = self.OUT
6262
self._esp.set_pin_mode(self.pin_id, 1)
6363
else:
64-
raise RuntimeError("Invalid mode defined")
64+
raise ValueError("Invalid mode defined")
6565

6666
def value(self, val=None):
6767
"""Sets ESP32 Pin GPIO output mode.
@@ -76,7 +76,7 @@ def value(self, val=None):
7676
self._value = val
7777
self._esp.set_digital_write(self.pin_id, 1)
7878
else:
79-
raise RuntimeError("Invalid value for pin")
79+
raise ValueError("Invalid value for pin")
8080
else:
8181
raise NotImplementedError(
8282
"digitalRead not currently implemented in esp32spi"

examples/esp32spi_aio_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
response.close()
6767
counter = counter + 1
6868
print("OK")
69-
except (ValueError, RuntimeError) as e:
69+
except OSError as e:
7070
print("Failed to get data, retrying\n", e)
7171
wifi.reset()
7272
continue

examples/esp32spi_cheerlights.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
value = value[key]
5555
print(value)
5656
response.close()
57-
except (ValueError, RuntimeError) as e:
57+
except OSError as e:
5858
print("Failed to get data, retrying\n", e)
5959
wifi.reset()
6060
continue

examples/esp32spi_ipconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
while not esp.is_connected:
5050
try:
5151
esp.connect_AP(secrets["ssid"], secrets["password"])
52-
except RuntimeError as e:
52+
except OSError as e:
5353
print("could not connect to AP, retrying: ", e)
5454
continue
5555
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

examples/esp32spi_localtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
print("Fetching json from", TIME_API)
4343
response = wifi.get(TIME_API)
4444
break
45-
except (ValueError, RuntimeError) as e:
45+
except OSError as e:
4646
print("Failed to get data, retrying\n", e)
4747
continue
4848

examples/esp32spi_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
while not esp.is_connected:
6060
try:
6161
esp.connect_AP(secrets["ssid"], secrets["password"])
62-
except RuntimeError as e:
62+
except OSError as e:
6363
print("could not connect to AP, retrying: ", e)
6464
continue
6565
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

examples/esp32spi_simpletest_rp2040.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
while not esp.is_connected:
4343
try:
4444
esp.connect_AP(secrets["ssid"], secrets["password"])
45-
except RuntimeError as e:
45+
except OSError as e:
4646
print("could not connect to AP, retrying: ", e)
4747
continue
4848
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

examples/esp32spi_wpa2ent_aio_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
response.close()
6464
counter = counter + 1
6565
print("OK")
66-
except (ValueError, RuntimeError) as e:
66+
except OSError as e:
6767
print("Failed to get data, retrying\n", e)
6868
wifi.reset()
6969
continue

examples/gpio/esp32spi_gpio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def esp_init_pin_modes(din, dout):
9494
esp_init_pin_modes(ESP_D_R_PIN, ESP_D_W_PIN)
9595
esp_d_r_val = esp.set_digital_read(ESP_D_R_PIN)
9696
print("--> ESP read:", esp_d_r_val)
97-
except (RuntimeError, AssertionError) as e:
97+
except OSError as e:
9898
print("ESP32 Error", e)
9999
esp_reset_all()
100100

@@ -104,7 +104,7 @@ def esp_init_pin_modes(din, dout):
104104
esp_init_pin_modes(ESP_D_R_PIN, ESP_D_W_PIN)
105105
esp.set_digital_write(ESP_D_W_PIN, esp_d_w_val)
106106
print("ESP wrote:", esp_d_w_val, "--> Red LED")
107-
except (RuntimeError) as e:
107+
except OSError as e:
108108
print("ESP32 Error", e)
109109
esp_reset_all()
110110

@@ -121,7 +121,7 @@ def esp_init_pin_modes(din, dout):
121121
"v)",
122122
sep="",
123123
)
124-
except (RuntimeError, AssertionError) as e:
124+
except OSError as e:
125125
print("ESP32 Error", e)
126126
esp_reset_all()
127127

@@ -166,7 +166,7 @@ def esp_init_pin_modes(din, dout):
166166
sep="",
167167
)
168168

169-
except (RuntimeError) as e:
169+
except OSError as e:
170170
print("ESP32 Error", e)
171171
esp_reset_all()
172172

examples/server/esp32spi_wsgiserver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def led_color(environ): # pylint: disable=unused-argument
213213
static
214214
)
215215
)
216-
except (OSError) as e:
216+
except OSError as e:
217217
raise RuntimeError(
218218
"""
219219
This example depends on a static asset directory.
@@ -240,7 +240,7 @@ def led_color(environ): # pylint: disable=unused-argument
240240
try:
241241
wsgiServer.update_poll()
242242
# Could do any other background tasks here, like reading sensors
243-
except (ValueError, RuntimeError) as e:
243+
except OSError as e:
244244
print("Failed to update server, restarting ESP32\n", e)
245245
wifi.reset()
246246
continue

0 commit comments

Comments
 (0)