Skip to content

Commit

Permalink
Improve error message when failing to find the body element in a docu…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
mwilliamson committed Feb 18, 2024
1 parent 7194953 commit 0459e2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions lib/docx/document-xml-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
14 changes: 14 additions & 0 deletions test/docx/document-xml-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?/);
});

0 comments on commit 0459e2c

Please sign in to comment.