From 73636e052a9006820d2bdf8a04f8e1d9989be94b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 9 May 2020 11:58:09 +0200 Subject: [PATCH] Handle errors individually for each annotation in the `_parsedAnnotations` getter While working on PR 11872, it occurred to me that it probably wouldn't be a bad idea to change the `_parsedAnnotations` getter to handle errors individually for each annotation. This way, one broken/corrupt annotation won't prevent the rest of them from being e.g. fetched through the API. --- src/core/document.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 8f5872cd1..b3d3de419 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -393,30 +393,24 @@ class Page { const parsedAnnotations = this.pdfManager .ensure(this, "annotations") .then(() => { - const annotationRefs = this.annotations; const annotationPromises = []; - for (let i = 0, ii = annotationRefs.length; i < ii; i++) { + for (const annotationRef of this.annotations) { annotationPromises.push( AnnotationFactory.create( this.xref, - annotationRefs[i], + annotationRef, this.pdfManager, this.idFactory - ) + ).catch(function (reason) { + warn(`_parsedAnnotations: "${reason}".`); + return null; + }) ); } - return Promise.all(annotationPromises).then( - function (annotations) { - return annotations.filter(function isDefined(annotation) { - return !!annotation; - }); - }, - function (reason) { - warn(`_parsedAnnotations: "${reason}".`); - return []; - } - ); + return Promise.all(annotationPromises).then(function (annotations) { + return annotations.filter(annotation => !!annotation); + }); }); return shadow(this, "_parsedAnnotations", parsedAnnotations);