File tree Expand file tree Collapse file tree
challenges/computing-101/building-a-web-server/common Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020import atexit
2121
2222import requests
23+ import urllib3
2324
2425
2526config = (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+
320334def 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
324344def random_data ():
You can’t perform that action at this time.
0 commit comments