Implement error handling and improve documentation in get_file_by_url
authorAlexander Cecile <redacted>
Tue, 25 Aug 2020 22:28:21 +0000 (18:28 -0400)
committerAlexander Cecile <redacted>
Tue, 25 Aug 2020 22:28:21 +0000 (18:28 -0400)
updateHostsFile.py

index cee883237afc8f45aa6be307579b5a1670efd071..2a4bce297127adc6a5184ea800805f9c204a75bc 100644 (file)
@@ -1474,19 +1474,37 @@ def get_file_by_url(url, params=None, **kwargs):
 
     Parameters
     ----------
-    url
-    params
-    kwargs
+    url : str or bytes
+        URL for the new Request object.
+    params :
+        Dictionary, list of tuples or bytes to send in the query string for the Request.
+    kwargs :
+        Optional arguments that request takes.
 
     Returns
     -------
-    content: str
+    url_data : str or None
+        The data retrieved at that URL from the file. Returns None if the
+        attempted retrieval is unsuccessful.
     """
 
-    req = requests.get(url=url, params=params, **kwargs)
+    try:
+        req = requests.get(url=url, params=params, **kwargs)
+    except requests.exceptions.RequestException:
+        print("Error retrieving data from {}".format(url))
+        return None
+
     req.encoding = req.apparent_encoding
-    res_text = "\n".join([domain_to_idna(line) for line in req.text.split("\n")])
-    return res_text
+
+    try:
+        res_text = req.text
+    except UnicodeDecodeError:
+        print("Decoding error when retrieving data from {}".format(url))
+        return None
+
+    res = "\n".join([domain_to_idna(line) for line in res_text.split("\n")])
+
+    return res
 
 
 def write_data(f, data):
git clone https://git.99rst.org/PROJECT