Don't clutter go_to_gi() with error logging.

This commit is contained in:
Solderpunk 2023-11-13 23:58:12 +01:00
parent cdb2b0282c
commit c6886d7eb9
1 changed files with 26 additions and 17 deletions

43
av98.py
View File

@ -338,22 +338,7 @@ you'll be able to transparently follow links to Gopherspace!""")
except UserAbortException:
return
except Exception as err:
# Print an error message
if isinstance(err, socket.gaierror):
self.log["dns_failures"] += 1
print("ERROR: DNS error!")
elif isinstance(err, ConnectionRefusedError):
self.log["refused_connections"] += 1
print("ERROR: Connection refused!")
elif isinstance(err, ConnectionResetError):
self.log["reset_connections"] += 1
print("ERROR: Connection reset!")
elif isinstance(err, (TimeoutError, socket.timeout)):
self.log["timeouts"] += 1
print("""ERROR: Connection timed out!
Slow internet connection? Use 'set timeout' to be more patient.""")
else:
print("ERROR: " + str(err))
self._print_friendly_error(err)
return
# Pass file to handler, unless we were asked not to
@ -374,6 +359,19 @@ you'll be able to transparently follow links to Gopherspace!""")
if update_hist:
self._update_history(gi)
def _print_friendly_error(self, err):
if isinstance(err, socket.gaierror):
ui_out.error("ERROR: DNS error!")
elif isinstance(err, ConnectionRefusedError):
ui_out.error("ERROR: Connection refused!")
elif isinstance(err, ConnectionResetError):
ui_out.error("ERROR: Connection reset!")
elif isinstance(err, (TimeoutError, socket.timeout)):
ui_out.error("""ERROR: Connection timed out!
Slow internet connection? Use 'set timeout' to be more patient.""")
else:
ui_out.error("ERROR: " + str(err))
def _fetch_over_network(self, gi):
# Be careful with client certificates!
@ -411,7 +409,18 @@ you'll be able to transparently follow links to Gopherspace!""")
if not gi.host:
address, f = None, open(gi.path, "rb")
else:
address, f = self._send_request(gi)
try:
address, f = self._send_request(gi)
except Exception as err:
if isinstance(err, socket.gaierror):
self.log["dns_failures"] += 1
elif isinstance(err, ConnectionRefusedError):
self.log["refused_connections"] += 1
elif isinstance(err, ConnectionResetError):
self.log["reset_connections"] += 1
elif isinstance(err, (TimeoutError, socket.timeout)):
self.log["timeouts"] += 1
raise err
# Spec dictates <META> should not exceed 1024 bytes,
# so maximum valid header length is 1027 bytes.