diff --git a/CHANGELOG b/CHANGELOG index 17808c30..b9fa0edd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changelog ========= +* 0.3.8 + * If zipfile fails to open the passed in file, we are now raising a + `MalformedDocxException` instead of a `BadZipFIle`. * 0.3.7 * Some inline tags (most notably the underline tag) could have a `val` of `none` and that would signify that the style is disabled. A `val` of diff --git a/README.rst b/README.rst index 2f750299..531a3ec2 100644 --- a/README.rst +++ b/README.rst @@ -222,6 +222,11 @@ The base parser `Docx2Html` relies on certain css class being set for certain be * class `pydocx-strike` -> Strike a line through. * class `pydocx-hidden` -> Hide the text. +Exceptions +########## + +Right now there is only one custom exception (`MalformedDocxException`). It is raised if either the `xml` or `zipfile` libraries raise an exception. + Optional Arguments ################## diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 5608c504..f78963ad 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -14,6 +14,7 @@ find_ancestor_with_tag, has_descendant_with_tag, ) +from pydocx.exceptions import MalformedDocxException logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("NewParser") @@ -37,7 +38,10 @@ @contextmanager def ZipFile(path): # This is not needed in python 3.2+ - f = zipfile.ZipFile(path) + try: + f = zipfile.ZipFile(path) + except zipfile.BadZipfile: + raise MalformedDocxException('Passed in document is not a docx') yield f f.close() diff --git a/pydocx/tests/test_docx.py b/pydocx/tests/test_docx.py index f5b39995..3ec4b1f7 100644 --- a/pydocx/tests/test_docx.py +++ b/pydocx/tests/test_docx.py @@ -1,12 +1,14 @@ import base64 - from os import path +from tempfile import NamedTemporaryFile from nose.plugins.skip import SkipTest +from nose.tools import raises from pydocx.tests import assert_html_equal, BASE_HTML from pydocx.parsers.Docx2Html import Docx2Html from pydocx.DocxParser import ZipFile +from pydocx.exceptions import MalformedDocxException def convert(path, *args, **kwargs): @@ -749,6 +751,12 @@ def test_missing_numbering(): ''') +@raises(MalformedDocxException) +def test_malformed_docx_exception(): + with NamedTemporaryFile(suffix='.docx') as f: + convert(f.name) + + def _converter(*args, **kwargs): # Having a converter that does nothing is the same as if abiword fails to # convert.