Skip to content

Commit 426821e

Browse files
committed
Better handling of download errors.
1 parent a05f2af commit 426821e

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

PyFunceble/cli/utils/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
from datetime import datetime, timezone
5656

5757
import colorama
58-
import requests
5958
from box import Box
6059

6160
import PyFunceble.cli.storage
@@ -65,6 +64,7 @@
6564
from PyFunceble.converter.internal_url import InternalUrlConverter
6665
from PyFunceble.helpers.dict import DictHelper
6766
from PyFunceble.helpers.download import DownloadHelper
67+
from PyFunceble.helpers.exceptions import UnableToDownload
6868
from PyFunceble.utils.version import VersionUtility
6969

7070

@@ -86,7 +86,7 @@ def get_upstream_version() -> Box:
8686
else True
8787
),
8888
).download_text()
89-
except requests.exceptions.RequestException:
89+
except UnableToDownload:
9090
response = "{}"
9191

9292
return Box(

PyFunceble/helpers/download.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,21 @@ def download_text(
363363
:raise UnableToDownload: When could not unable to download the URL.
364364
"""
365365

366-
req = self.session.get(self.url, verify=self.certificate_validation)
366+
try:
367+
req = self.session.get(self.url, verify=self.certificate_validation)
367368

368-
if req.status_code == 200:
369-
response = req.text
369+
if req.status_code == 200:
370+
response = req.text
370371

371-
if destination and isinstance(destination, str):
372-
FileHelper(destination).write(req.text, overwrite=True)
372+
if destination and isinstance(destination, str):
373+
FileHelper(destination).write(req.text, overwrite=True)
373374

374-
return response
375+
return response
375376

376-
raise PyFunceble.helpers.exceptions.UnableToDownload(
377-
f"{req.url} (retries: {self.retries} | status code: {req.status_code})"
378-
)
377+
raise PyFunceble.helpers.exceptions.UnableToDownload(
378+
f"{req.url} (retries: {self.retries} | status code: {req.status_code})"
379+
)
380+
except requests.exceptions.RequestException as exception:
381+
raise PyFunceble.helpers.exceptions.UnableToDownload(
382+
f"{self.url} (could not resolve?)"
383+
) from exception

0 commit comments

Comments
 (0)