diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-02-18 10:51:56 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-02-18 10:51:56 +0000 |
| commit | 0459e2cf53ad8670e6692020f5864eafffe2caf9 (patch) | |
| tree | 696066f4fe3b9f66abcc29386d9be73ac0c31c5d | |
| parent | 7194953e90ab7a24b49f471b23cc30d03beba517 (diff) | |
Improve error message when failing to find the body element in a document
| -rw-r--r-- | NEWS | 2 | ||||
| -rw-r--r-- | lib/docx/document-xml-reader.js | 4 | ||||
| -rw-r--r-- | test/docx/document-xml-reader.tests.js | 14 |
3 files changed, 20 insertions, 0 deletions
@@ -2,6 +2,8 @@ * Support attributes in HTML paths in style mappings. +* Improve error message when failing to find the body element in a document. + # 1.6.0 * Add transformDocument to the TypeScript declarations. diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index c5472c2..9543ed8 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -10,6 +10,10 @@ function DocumentXmlReader(options) { function convertXmlToDocument(element) { var body = element.first("w:body"); + if (body == null) { + throw new Error("Could not find the body element: are you sure this is a docx file?"); + } + var result = bodyReader.readXmlElements(body.children) .map(function(children) { return new documents.Document(children, { diff --git a/test/docx/document-xml-reader.tests.js b/test/docx/document-xml-reader.tests.js index 08389e1..10c8df1 100644 --- a/test/docx/document-xml-reader.tests.js +++ b/test/docx/document-xml-reader.tests.js @@ -23,3 +23,17 @@ test("when body element is present then body is read", function() { {} )); }); + +test("when body element is not present then error is thrown", function() { + var bodyReader = createBodyReaderForTests({}); + var documentXmlReader = new DocumentXmlReader({ + bodyReader: bodyReader + }); + var paragraphXml = xml.element("w:p", {}, []); + var bodyXml = xml.element("w:body2", {}, [paragraphXml]); + var documentXml = xml.element("w:document", {}, [bodyXml]); + + assert.throws(function() { + documentXmlReader.convertXmlToDocument(documentXml); + }, /Could not find the body element: are you sure this is a docx file?/); +}); |
