diff --git a/gallery_dl/exception.py b/gallery_dl/exception.py index 251c97a8..ede5dd7d 100644 --- a/gallery_dl/exception.py +++ b/gallery_dl/exception.py @@ -18,6 +18,7 @@ Exception | +-- NotFoundError | +-- HttpError +-- NoExtractorError + +-- FormatError +-- StopExtraction """ @@ -50,5 +51,9 @@ class NoExtractorError(GalleryDLException): """No extractor can handle the given URL""" +class FormatError(GalleryDLException): + """Error while building output path""" + + class StopExtraction(GalleryDLException): """Extraction should stop""" diff --git a/gallery_dl/job.py b/gallery_dl/job.py index f5916579..4b022faf 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -54,6 +54,10 @@ class Job(): log.error("The %s at '%s' does not exist", res, self.url) except exception.HttpError as exc: log.error("HTTP request failed:\n%s", exc) + except exception.FormatError as exc: + err, obj = exc.args + log.error("Applying %s format string failed:\n%s: %s", + obj, err.__class__.__name__, err) except exception.StopExtraction: pass except OSError as exc: diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 12196162..98342cba 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -183,10 +183,14 @@ class PathFormat(): def set_directory(self, keywords): """Build directory path and create it if necessary""" - segments = [ - text.clean_path(segment.format_map(keywords).strip()) - for segment in self.directory_fmt - ] + try: + segments = [ + text.clean_path(segment.format_map(keywords).strip()) + for segment in self.directory_fmt + ] + except Exception as exc: + raise exception.FormatError(exc, "directory") + self.directory = os.path.join( self.get_base_directory(), *segments @@ -209,7 +213,12 @@ class PathFormat(): def build_path(self, sep=os.path.sep): """Use filename-keywords and directory to build a full path""" - filename = text.clean_path(self.filename_fmt.format_map(self.keywords)) + try: + filename = text.clean_path( + self.filename_fmt.format_map(self.keywords)) + except Exception as exc: + raise exception.FormatError(exc, "filename") + self.path = self.directory + sep + filename self.realpath = self.realdirectory + sep + filename