From 9b7081966c134b8737aa3dfc313aa14ce66b595c Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Sun, 21 Feb 2016 20:16:50 +0000 Subject: [PATCH] Better error messages --- ttsmirror.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ttsmirror.py b/ttsmirror.py index 2d10b52..10f3682 100644 --- a/ttsmirror.py +++ b/ttsmirror.py @@ -9,6 +9,28 @@ __version__ = '0.1' __author__ = 'Andrew Williams' +class MirrorExceptionBase(Exception): + """Base exception for all mirroring issues""" + def __init__(self, msg): + self.msg = msg + + def __unicode__(self): + return self.msg + + def __str__(self): + return self.__unicode__() + + +class MissingFileExecption(MirrorExceptionBase): + """Raised when a source file is missing""" + pass + + +class UnknownErrorException(MirrorExceptionBase): + """Raised when an unknown issue is encountered when mirroring""" + pass + + def iterate_save(obj, output_path, url_prefix, hash_filename=False): """ Iterate a save, download assets, and update the locations as needed. @@ -46,7 +68,9 @@ def iterate_save(obj, output_path, url_prefix, hash_filename=False): for chunk in res: outfile.write(chunk) if res.status_code == 404: - raise Exception + raise MissingFileExecption('%s returned a 404' % val) + else: + raise UnknownErrorException('URL %s returned code %d' % (val, res.status_code)) obj[key] = urljoin(url_prefix, new_filename) return obj @@ -60,6 +84,7 @@ def process_save(filename, output_path, url_prefix, hash_filename): new_save = iterate_save(save, output_path, url_prefix, hash_filename) except Exception as e: logging.exception('Unable to process save: %s' % e) + return new_save['SaveName'] = '%s - Mirrored' % new_save['SaveName'] with open(new_filename, 'w') as outfobj: outfobj.write(json.dumps(new_save, sort_keys=True, indent=4, separators=(',', ': ')))