Checking for 404 error and valid json format

This commit is contained in:
Alexandre Aubin 2017-02-07 08:18:52 -05:00
parent 991b64db92
commit a61445c9c3
2 changed files with 15 additions and 2 deletions

View file

@ -31,6 +31,7 @@
"appslist_fetched": "The app list has been fetched",
"appslist_removed": "The app list has been removed",
"appslist_retrieve_error": "Unable to retrieve the remote app list: {error}",
"appslist_retrieve_bad_format": "Retrieved file is not a valid app list",
"appslist_unknown": "Unknown app list",
"ask_current_admin_password": "Current administration password",
"ask_email": "Email address",

View file

@ -100,10 +100,22 @@ def app_fetchlist(url=None, name=None):
# Download file
try:
applist = requests.get(url, timeout=30).text
applist_request = requests.get(url, timeout=30)
except Exception as e:
raise MoulinetteError(errno.EBADR, m18n.n('appslist_retrieve_error', error=str(e)))
if (applist_request.status_code != 200):
raise MoulinetteError(errno.EBADR, m18n.n('appslist_retrieve_error', error="404, not found"))
# Validate app list format
# TODO / Possible improvement : better validation for app list (check that
# json fields actually look like an app list and not any json file)
applist = applist_request.text
try:
json.loads(applist)
except ValueError, e:
raise MoulinetteError(errno.EBADR, m18n.n('appslist_retrieve_bad_format'))
# Write app list to file
list_file = '%s/%s.json' % (repo_path, name)
with open(list_file, "w") as f: