diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-08-28 19:44:42 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-08-28 19:44:42 +0100 |
| commit | c42714e5f4b643de27b106a084601e39b7e5d04c (patch) | |
| tree | 7c00b6dd3b97941c738a8ea0f0282770452d58b6 | |
| parent | 578ce38c2922c3a5b5ed2316d885e450ba3a0d6c (diff) | |
Throw more informative error when word/document.xml cannot be found
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | lib/docx/docx-reader.js | 20 | ||||
| -rw-r--r-- | test/mammoth.tests.js | 10 | ||||
| -rw-r--r-- | test/test-data/empty.zip | bin | 0 -> 22 bytes |
4 files changed, 26 insertions, 8 deletions
@@ -1,3 +1,7 @@ +# 1.2.0 + +* Throw more informative error when word/document.xml cannot be found. + # 1.1.0 * Add support for comments. diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 57830d7..0c7d57f 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -60,16 +60,20 @@ function read(docxFile, input) { }; }).then(function(result) { return readXmlFileWithBody("document", result, function(bodyReader, xml) { - return result.notes.flatMap(function(notes) { - return result.comments.flatMap(function(comments) { - var reader = new DocumentXmlReader({ - bodyReader: bodyReader, - notes: notes, - comments: comments + if (xml) { + return result.notes.flatMap(function(notes) { + return result.comments.flatMap(function(comments) { + var reader = new DocumentXmlReader({ + bodyReader: bodyReader, + notes: notes, + comments: comments + }); + return reader.convertXmlToDocument(xml); }); - return reader.convertXmlToDocument(xml); }); - }); + } else { + throw new Error("Could not find word/document.xml in ZIP file. Are you sure this is a valid .docx file?"); + } }); }); } diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index f4d445b..bf3d2fd 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -368,3 +368,13 @@ test('extractRawText can use .docx files represented by a Buffer', function() { assert.deepEqual(result.messages, []); }); }); + + +test('should throw error if file is not a valid docx document', function() { + var docxPath = path.join(__dirname, "test-data/empty.zip"); + return mammoth.convertToHtml({path: docxPath}).then(function(result) { + assert.ok(false, "Expected error"); + }, function(error) { + assert.equal(error.message, "Could not find word/document.xml in ZIP file. Are you sure this is a valid .docx file?"); + }); +}); diff --git a/test/test-data/empty.zip b/test/test-data/empty.zip Binary files differnew file mode 100644 index 0000000..15cb0ec --- /dev/null +++ b/test/test-data/empty.zip |
