Skip to content

Commit 499f94f

Browse files
committed
Address PR feedback
1 parent 3ea42b8 commit 499f94f

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

  • challenges/computing-101/building-a-web-server/common

challenges/computing-101/building-a-web-server/common/run.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import atexit
2121

2222
import requests
23+
import urllib3
2324

2425

2526
config = (pathlib.Path(__file__).parent / ".config").read_text()
@@ -317,8 +318,27 @@ def retry_session():
317318
return session
318319

319320

321+
def request_timed_out(exc):
322+
if isinstance(exc, requests.exceptions.ConnectTimeout):
323+
return False
324+
if isinstance(exc, requests.exceptions.Timeout):
325+
return True
326+
# Retry wraps read timeouts as ConnectionError, so inspect the inner urllib3 reason.
327+
for arg in exc.args:
328+
reason = getattr(arg, "reason", None)
329+
if isinstance(reason, urllib3.exceptions.ReadTimeoutError):
330+
return True
331+
return False
332+
333+
320334
def request_failure(method, exc):
321-
return f"{method}: Failed to connect ({type(exc).__name__}: {exc})"
335+
if request_timed_out(exc):
336+
failure = "Timed out"
337+
elif isinstance(exc, requests.exceptions.ConnectionError):
338+
failure = "Failed to connect"
339+
else:
340+
failure = "Request failed"
341+
return f"{method}: {failure} ({type(exc).__name__}: {exc})"
322342

323343

324344
def random_data():

0 commit comments

Comments
 (0)