Skip to content

Commit bc1fa4b

Browse files
committed
Added user-friendly errors in failing network conditions
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
1 parent 771b9a7 commit bc1fa4b

5 files changed

Lines changed: 53 additions & 11 deletions

File tree

cfbs/analyze.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ def mpf_vcf_dicts(offline=False):
131131
"https://api.github.com/repos/" + REPO_OWNERNAME + "/releases/latest"
132132
)
133133

134-
latest_release_data = get_json(LATEST_RELEASE_API_URL)
134+
try:
135+
latest_release_data = get_json(LATEST_RELEASE_API_URL)
136+
except FetchError as e:
137+
user_error(
138+
"Downloading CFEngine release information failed - check your Wi-Fi / network settings."
139+
)
135140

136141
latest_release_name = latest_release_data["name"]
137142
ri_archive_url = REPO_URL + "/archive/refs/tags/" + latest_release_name + ".zip"

cfbs/commands.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from cfbs.args import get_args
1515

1616
from cfbs.utils import (
17+
FetchError,
1718
cfbs_dir,
1819
cfbs_filename,
1920
is_cfbs_repo,
@@ -905,7 +906,12 @@ def _download_dependencies(
905906
sh("git clone %s %s" % (url, commit_dir))
906907
sh("(cd %s && git checkout %s)" % (commit_dir, commit))
907908
else:
908-
versions = get_json(_VERSION_INDEX)
909+
try:
910+
versions = get_json(_VERSION_INDEX)
911+
except FetchError as e:
912+
user_error(
913+
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
914+
)
909915
try:
910916
checksum = versions[name][module["version"]]["archive_sha256"]
911917
except KeyError:

cfbs/index.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import OrderedDict
33

44
from cfbs.module import Module
5-
from cfbs.utils import get_or_read_json, user_error, get_json
5+
from cfbs.utils import FetchError, get_or_read_json, user_error, get_json
66
from cfbs.internal_file_management import local_module_name
77

88
_DEFAULT_INDEX = (
@@ -87,7 +87,13 @@ def _expand_index(self):
8787

8888
assert type(index) is str
8989

90-
self._data = get_or_read_json(index)
90+
try:
91+
self._data = get_or_read_json(index)
92+
except FetchError as e:
93+
user_error(
94+
"Downloading index '%s' failed - check your Wi-Fi / network settings."
95+
% index
96+
)
9197

9298
if not self._data:
9399
sys.exit("Could not download or find module index")
@@ -121,7 +127,13 @@ def exists(self, module):
121127
return True
122128
if not version:
123129
return name in self
124-
versions = get_json(_VERSION_INDEX)
130+
try:
131+
versions = get_json(_VERSION_INDEX)
132+
except FetchError as e:
133+
user_error(
134+
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
135+
)
136+
125137
return name in versions and version in versions[name]
126138

127139
def check_existence(self, modules: list):
@@ -162,7 +174,12 @@ def get_module_object(self, module, added_by=None):
162174
else:
163175
object = self[name]
164176
if version:
165-
versions = get_json(_VERSION_INDEX)
177+
try:
178+
versions = get_json(_VERSION_INDEX)
179+
except FetchError as e:
180+
user_error(
181+
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
182+
)
166183
new_values = versions[name][version]
167184
specifics = {
168185
k: v for (k, v) in new_values.items() if k in Module.attributes()

cfbs/masterfiles/download_all_versions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ def get_download_urls_enterprise(min_version=None):
1313

1414
print("* gathering download URLs...")
1515

16-
data = get_json(ENTERPRISE_RELEASES_URL)
16+
try:
17+
data = get_json(ENTERPRISE_RELEASES_URL)
18+
except FetchError as e:
19+
user_error(
20+
"Downloading CFEngine release data failed - check your Wi-Fi / network settings."
21+
)
1722

1823
for release_data in data["releases"]:
1924
version = release_data["version"]
@@ -38,7 +43,13 @@ def get_download_urls_enterprise(min_version=None):
3843
continue
3944

4045
release_url = release_data["URL"]
41-
subdata = get_json(release_url)
46+
try:
47+
subdata = get_json(release_url)
48+
except FetchError as e:
49+
user_error(
50+
"Downloading CFEngine release data for version %s failed - check your Wi-Fi / network settings."
51+
% version
52+
)
4253
artifacts_data = subdata["artifacts"]
4354

4455
if "Additional Assets" not in artifacts_data:

cfbs/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ def user_error(msg: str):
115115

116116

117117
def get_json(url: str) -> OrderedDict:
118-
with urllib.request.urlopen(url) as r:
119-
assert r.status >= 200 and r.status < 300
120-
return json.loads(r.read().decode(), object_pairs_hook=OrderedDict)
118+
try:
119+
with urllib.request.urlopen(url) as r:
120+
assert r.status >= 200 and r.status < 300
121+
return json.loads(r.read().decode(), object_pairs_hook=OrderedDict)
122+
except urllib.error.URLError as e:
123+
raise FetchError("Failed to get JSON from '%s'" % url) from e
121124

122125

123126
def get_or_read_json(path: str) -> OrderedDict:

0 commit comments

Comments
 (0)