Skip to content

Commit

Permalink
[FIX] - Better error handle errors in project update code
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Sep 27, 2018
1 parent 319073f commit 714e85e
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/roam/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import roam.config


class UpdateExpection(Exception):
pass


def add_slash(url):
if not url.endswith("/"):
url += "/"
Expand Down Expand Up @@ -103,7 +107,16 @@ def download_file(url, fileout):
Will open and write to fileout
"""
roam.utils.debug("Opening URL: {}".format(url))
result = urllib2.urlopen(url)
try:
result = urllib2.urlopen(url)
except urllib2.HTTPError as ex:
if ex.code == 404:
roam.utils.warning("Can't find URL: {}".format(url))
else:
roam.utils.exception("HTTP Error: {}".format(ex))
yield "Error in download"
raise UpdateExpection("Error in downloading file.")

length = result.headers['content-length']

length = int(length) / 1024 / 1024
Expand Down Expand Up @@ -184,6 +197,13 @@ def __init__(self, basefolder):
self.basefolder = basefolder
self.projectUpdateStatus.connect(self.status_updated)

def check_url_found(self, url):
try:
result = urllib2.urlopen(url)
return result.code == 200
except urllib2.HTTPError as ex:
return False

def fetch_data(self, rootfolder, filename, serverurl):
"""
Download the update zip file for the project from the server
Expand All @@ -197,9 +217,19 @@ def fetch_data(self, rootfolder, filename, serverurl):
filename = "{}.zip".format(filename)
url = urlparse.urljoin(serverurl, "projects/{}".format(filename))
zippath = os.path.join(tempfolder, filename)
roam.utils.info("Downloading project zip {}".format(url))
for status in download_file(url, zippath):
yield status
if not self.check_url_found(url):
yield "Skipping data download"
yield "Done"
return

roam.utils.info("Downloading data zip from {}".format(url))
try:
for status in download_file(url, zippath):
yield status
except UpdateExpection as ex:
roam.utils.exception("Error in update for project")
yield "Error in downloading data"
return

yield "Extracting data.."
with zipfile.ZipFile(zippath, "r") as z:
Expand Down

0 comments on commit 714e85e

Please sign in to comment.