Skip to content

Commit

Permalink
Merge pull request #164 from dannystaple/use-connection-errors
Browse files Browse the repository at this point in the history
Make these connection errors distinct from other error types.
  • Loading branch information
dhalbert authored Jun 21, 2022
2 parents a8c8f71 + 2cf1d45 commit f3d504b
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 71 deletions.
98 changes: 49 additions & 49 deletions adafruit_esp32spi/adafruit_esp32spi.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
"""Given a hostname and a port name, return a 'socket.getaddrinfo'
compatible list of tuples. Honestly, we ignore anything but host & port"""
if not isinstance(port, int):
raise RuntimeError("Port must be an integer")
raise ValueError("Port must be an integer")
ipaddr = _the_interface.get_host_by_name(host)
return [(AF_INET, socktype, proto, "", (ipaddr, port))]

Expand All @@ -56,7 +56,7 @@ def __init__(
self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, socknum=None
):
if family != AF_INET:
raise RuntimeError("Only AF_INET family supported")
raise ValueError("Only AF_INET family supported")
self._type = type
self._buffer = b""
self._socknum = socknum if socknum else _the_interface.get_socket()
Expand All @@ -74,7 +74,7 @@ def connect(self, address, conntype=None):
if not _the_interface.socket_connect(
self._socknum, host, port, conn_mode=conntype
):
raise RuntimeError("Failed to connect to host", host)
raise ConnectionError("Failed to connect to host", host)
self._buffer = b""

def send(self, data): # pylint: disable=no-self-use
Expand Down Expand Up @@ -105,7 +105,7 @@ def readline(self, eol=b"\r\n"):
self._buffer += _the_interface.socket_read(self._socknum, avail)
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout:
self.close() # Make sure to close socket so that we don't exhaust sockets.
raise RuntimeError("Didn't receive full response, failing out")
raise OSError("Didn't receive full response, failing out")
firstline, self._buffer = self._buffer.split(eol, 1)
gc.collect()
return firstline
Expand Down
6 changes: 3 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def connect_normal(self):
self.esp.connect_AP(bytes(ssid, "utf-8"), bytes(password, "utf-8"))
failure_count = 0
self.pixel_status((0, 100, 0))
except (ValueError, RuntimeError) as error:
except OSError as error:
print("Failed to connect, retrying\n", error)
failure_count += 1
if failure_count >= self.attempts:
Expand Down Expand Up @@ -173,7 +173,7 @@ def create_ap(self):
self.esp.create_AP(bytes(self.ssid, "utf-8"), None)
failure_count = 0
self.pixel_status((0, 100, 0))
except (ValueError, RuntimeError) as error:
except OSError as error:
print("Failed to create access point\n", error)
failure_count += 1
if failure_count >= self.attempts:
Expand Down Expand Up @@ -203,7 +203,7 @@ def connect_enterprise(self):
failure_count = 0
self.pixel_status((0, 100, 0))
sleep(1)
except (ValueError, RuntimeError) as error:
except OSError as error:
print("Failed to connect, retrying\n", error)
failure_count += 1
if failure_count >= self.attempts:
Expand Down
4 changes: 2 additions & 2 deletions adafruit_esp32spi/digitalio.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def init(self, mode=IN):
self._mode = self.OUT
self._esp.set_pin_mode(self.pin_id, 1)
else:
raise RuntimeError("Invalid mode defined")
raise ValueError("Invalid mode defined")

def value(self, val=None):
"""Sets ESP32 Pin GPIO output mode.
Expand All @@ -76,7 +76,7 @@ def value(self, val=None):
self._value = val
self._esp.set_digital_write(self.pin_id, 1)
else:
raise RuntimeError("Invalid value for pin")
raise ValueError("Invalid value for pin")
else:
raise NotImplementedError(
"digitalRead not currently implemented in esp32spi"
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_aio_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
response.close()
counter = counter + 1
print("OK")
except (ValueError, RuntimeError) as e:
except OSError as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_cheerlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
value = value[key]
print(value)
response.close()
except (ValueError, RuntimeError) as e:
except OSError as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_ipconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_localtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
print("Fetching json from", TIME_API)
response = wifi.get(TIME_API)
break
except (ValueError, RuntimeError) as e:
except OSError as e:
print("Failed to get data, retrying\n", e)
continue

Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_simpletest_rp2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32spi_wpa2ent_aio_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
response.close()
counter = counter + 1
print("OK")
except (ValueError, RuntimeError) as e:
except OSError as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue
Expand Down
8 changes: 4 additions & 4 deletions examples/gpio/esp32spi_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def esp_init_pin_modes(din, dout):
esp_init_pin_modes(ESP_D_R_PIN, ESP_D_W_PIN)
esp_d_r_val = esp.set_digital_read(ESP_D_R_PIN)
print("--> ESP read:", esp_d_r_val)
except (RuntimeError, AssertionError) as e:
except OSError as e:
print("ESP32 Error", e)
esp_reset_all()

Expand All @@ -104,7 +104,7 @@ def esp_init_pin_modes(din, dout):
esp_init_pin_modes(ESP_D_R_PIN, ESP_D_W_PIN)
esp.set_digital_write(ESP_D_W_PIN, esp_d_w_val)
print("ESP wrote:", esp_d_w_val, "--> Red LED")
except (RuntimeError) as e:
except OSError as e:
print("ESP32 Error", e)
esp_reset_all()

Expand All @@ -121,7 +121,7 @@ def esp_init_pin_modes(din, dout):
"v)",
sep="",
)
except (RuntimeError, AssertionError) as e:
except OSError as e:
print("ESP32 Error", e)
esp_reset_all()

Expand Down Expand Up @@ -166,7 +166,7 @@ def esp_init_pin_modes(din, dout):
sep="",
)

except (RuntimeError) as e:
except OSError as e:
print("ESP32 Error", e)
esp_reset_all()

Expand Down
4 changes: 2 additions & 2 deletions examples/server/esp32spi_wsgiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def led_color(environ): # pylint: disable=unused-argument
static
)
)
except (OSError) as e:
except OSError as e:
raise RuntimeError(
"""
This example depends on a static asset directory.
Expand All @@ -240,7 +240,7 @@ def led_color(environ): # pylint: disable=unused-argument
try:
wsgiServer.update_poll()
# Could do any other background tasks here, like reading sensors
except (ValueError, RuntimeError) as e:
except OSError as e:
print("Failed to update server, restarting ESP32\n", e)
wifi.reset()
continue

0 comments on commit f3d504b

Please sign in to comment.