From 1a9660a7f204d4098e5dc8809dd33c433d0571bc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 23 Jan 2018 21:31:20 +0000 Subject: Throw exception immediately when document filename is not found --- lib/docx/docx-reader.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 898da1d..9570dd5 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -61,22 +61,18 @@ function read(docxFile, input) { }; }).then(function(result) { var documentFilename = findDocumentFilename(docxFile, result.fileRelationships); - if (documentFilename) { - return readXmlFileWithBody(documentFilename, 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 - }); - return reader.convertXmlToDocument(xml); + return readXmlFileWithBody(documentFilename, 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 }); + return reader.convertXmlToDocument(xml); }); }); - } else { - throw new Error("Could not find main document part. Are you sure this is a valid .docx file?"); - } + }); }); } @@ -89,7 +85,11 @@ function findDocumentFilename(docxFile, relationships) { var validTargets = normalisedTargets.filter(function(target) { return docxFile.exists(target); }); - return validTargets[0]; + if (validTargets.length === 0) { + throw new Error("Could not find main document part. Are you sure this is a valid .docx file?"); + } else { + return validTargets[0]; + } } function stripPrefix(value, prefix) { -- cgit v1.2.3