Skip to content

Commit

Permalink
Improve error handling between thumbnaildownload and executable
Browse files Browse the repository at this point in the history
thumbnaildownload will intercept any unexpected Exception,
log it as Critical, and raise a DownloadException.

Executable commonsdownloader will catch any DownloadException
and raise an error.
  • Loading branch information
JeanFred committed Jan 17, 2015
1 parent d6b7d29 commit 3e1c728
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 6 additions & 3 deletions commonsdownloader/commonsdownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import logging
import argparse
from thumbnaildownload import download_file
from thumbnaildownload import download_file, DownloadException
from itertools import izip_longest


Expand Down Expand Up @@ -76,8 +76,11 @@ def download_files_if_not_in_cache(files_iterator, output_path):
if is_file_in_cache(file_name, width, local_cache):
logging.info('Skipping file %s' % file_name)
continue
download_file(file_name, output_path, width=width)
write_file_to_cache(file_name, width, cache_fh)
try:
download_file(file_name, output_path, width=width)
write_file_to_cache(file_name, width, cache_fh)
except DownloadException, e:
logging.error("Could not download %s: %s", file_name, e.message)


class Folder(argparse.Action):
Expand Down
14 changes: 10 additions & 4 deletions commonsdownloader/thumbnaildownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ def download_file(image_name, output_path, width=DEFAULT_WIDTH):
logging.info("Requested width is bigger than source - downloading full size")
contents, output_file_name = get_full_size_file(image_name)
output_file_path = os.path.join(output_path, output_file_name)
with open(output_file_path, 'wb') as f:
logging.debug("Writing as %s" % output_file_path)
f.write(contents)
return output_file_path
try:
with open(output_file_path, 'wb') as f:
logging.debug("Writing as %s" % output_file_path)
f.write(contents)
return output_file_path
except Exception, e:
logging.critical(e.message)
msg = 'An unexpected error occured when downloading %s to %s: %s' % \
(image_name, output_path, e.message)
raise DownloadException(msg)

0 comments on commit 3e1c728

Please sign in to comment.