Skip to content

Commit

Permalink
Raise exception when file cannot be written on disk
Browse files Browse the repository at this point in the history
If the file cannot be written on disk, raising an IOError,
then we log it as error and raise a DownloadException to
be handled be the caller.

Add new Exception CouldNotWriteFileOnDiskException.

Addresses #8.
  • Loading branch information
JeanFred committed Jan 17, 2015
1 parent 3e1c728 commit 7dad7e6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions commonsdownloader/thumbnaildownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class RequestedWidthBiggerThanSourceException(DownloadException):
pass


class CouldNotWriteFileOnDiskException(DownloadException):

"""Exception raised when the file could not be written on disk."""

pass


def clean_up_filename(file_name):
"""Return the cleaned-up file title."""
return file_name.strip().replace(' ', '_')
Expand Down Expand Up @@ -124,6 +131,11 @@ def download_file(image_name, output_path, width=DEFAULT_WIDTH):
logging.debug("Writing as %s" % output_file_path)
f.write(contents)
return output_file_path
except IOError, e:
msg = 'Could not write file %s on disk to %s: %s' % \
(image_name, output_path, e.message)
logging.error(msg)
raise CouldNotWriteFileOnDiskException(msg)
except Exception, e:
logging.critical(e.message)
msg = 'An unexpected error occured when downloading %s to %s: %s' % \
Expand Down

0 comments on commit 7dad7e6

Please sign in to comment.