Update Catalog.getAllPageDicts to always propagate the actual Errors (PR 14335 follow-up)

Rather than "swallowing" the actual Errors, when data fetching fails, ensure that they're always being propagated as intended to the call-site instead.
Note that we purposely handle `XRefEntryException` specially, to make it possible to fallback to indexing all XRef objects.
This commit is contained in:
Jonas Jenwald 2021-12-10 13:54:34 +01:00
parent 47f9eef584
commit 70ac6b1694
2 changed files with 21 additions and 9 deletions

View File

@ -1224,7 +1224,7 @@ class Catalog {
* Eagerly fetches the entire /Pages-tree; should ONLY be used as a fallback. * Eagerly fetches the entire /Pages-tree; should ONLY be used as a fallback.
* @returns {Map} * @returns {Map}
*/ */
getAllPageDicts() { getAllPageDicts(recoveryMode = false) {
const queue = [{ currentNode: this.toplevelPagesDict, posInKids: 0 }]; const queue = [{ currentNode: this.toplevelPagesDict, posInKids: 0 }];
const visitedNodes = new RefSet(); const visitedNodes = new RefSet();
const map = new Map(); const map = new Map();
@ -1233,8 +1233,8 @@ class Catalog {
function addPageDict(pageDict, pageRef) { function addPageDict(pageDict, pageRef) {
map.set(pageIndex++, [pageDict, pageRef]); map.set(pageIndex++, [pageDict, pageRef]);
} }
function addPageError(msg) { function addPageError(error) {
map.set(pageIndex++, [new FormatError(msg), null]); map.set(pageIndex++, [error, null]);
} }
while (queue.length > 0) { while (queue.length > 0) {
@ -1248,12 +1248,16 @@ class Catalog {
if (ex instanceof MissingDataException) { if (ex instanceof MissingDataException) {
throw ex; throw ex;
} }
if (ex instanceof XRefEntryException) { if (ex instanceof XRefEntryException && !recoveryMode) {
throw ex; throw ex;
} }
addPageError(ex);
break;
} }
if (!Array.isArray(kids)) { if (!Array.isArray(kids)) {
addPageError("Page dictionary kids object is not an array."); addPageError(
new FormatError("Page dictionary kids object is not an array.")
);
break; break;
} }
@ -1271,13 +1275,17 @@ class Catalog {
if (ex instanceof MissingDataException) { if (ex instanceof MissingDataException) {
throw ex; throw ex;
} }
if (ex instanceof XRefEntryException) { if (ex instanceof XRefEntryException && !recoveryMode) {
throw ex; throw ex;
} }
addPageError(ex);
break;
} }
// Prevent circular references in the /Pages tree. // Prevent circular references in the /Pages tree.
if (visitedNodes.has(kidObj)) { if (visitedNodes.has(kidObj)) {
addPageError("Pages tree contains circular reference."); addPageError(
new FormatError("Pages tree contains circular reference.")
);
break; break;
} }
visitedNodes.put(kidObj); visitedNodes.put(kidObj);
@ -1289,7 +1297,9 @@ class Catalog {
} }
if (!(obj instanceof Dict)) { if (!(obj instanceof Dict)) {
addPageError( addPageError(
"Page dictionary kid reference points to wrong type of object." new FormatError(
"Page dictionary kid reference points to wrong type of object."
)
); );
break; break;
} }

View File

@ -1393,7 +1393,9 @@ class PDFDocument {
let pagesTree; let pagesTree;
try { try {
pagesTree = await pdfManager.ensureCatalog("getAllPageDicts"); pagesTree = await pdfManager.ensureCatalog("getAllPageDicts", [
recoveryMode,
]);
} catch (reasonAll) { } catch (reasonAll) {
if (reasonAll instanceof XRefEntryException && !recoveryMode) { if (reasonAll instanceof XRefEntryException && !recoveryMode) {
throw new XRefParseException(); throw new XRefParseException();