From 256806a3dd10ccdfb1295181d09af7655c05fa29 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Sep 2015 18:08:58 +0100 Subject: Capture errors on trying to open file URIs --- mammoth/docx/files.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'mammoth') diff --git a/mammoth/docx/files.py b/mammoth/docx/files.py index bde488d..dc5146e 100644 --- a/mammoth/docx/files.py +++ b/mammoth/docx/files.py @@ -19,17 +19,17 @@ class Files(object): pass def open(self, uri): - if _is_absolute(uri): - return contextlib.closing(urlopen(uri)) - elif self._base is not None: - try: + try: + if _is_absolute(uri): + return contextlib.closing(urlopen(uri)) + elif self._base is not None: return open(os.path.join(self._base, uri), "rb") - except IOError as error: - message = "could not open external image '{0}' from document directory '{1}'\n{2}".format( - uri, self._base, str(error)) - raise InvalidFileReferenceError(message) - else: - raise InvalidFileReferenceError("could not find external image '{0}', fileobj has no name".format(uri)) + else: + raise InvalidFileReferenceError("could not find external image '{0}', fileobj has no name".format(uri)) + except IOError as error: + message = "could not open external image: '{0}' (document directory: '{1}')\n{2}".format( + uri, self._base, str(error)) + raise InvalidFileReferenceError(message) def _is_absolute(url): -- cgit v1.2.3