Improve error handling in the PDFFindController.prototype.#extractText method

This handles *all* errors correctly, if e.g. the document is closed in the middle of searching.
Also, replacing the "promise chains" with more `await` helps simplify the code a little bit.
This commit is contained in:
Jonas Jenwald 2026-04-29 22:13:30 +02:00
parent 34c3ee16f8
commit a6e2a42df1

View File

@ -906,40 +906,27 @@ class PDFFindController {
resolve();
return;
}
await pdfDoc
.getPage(i + 1)
.then(pdfPage => pdfPage.getTextContent(textOptions))
.then(
textContent => {
const strBuf = [];
try {
const pdfPage = await pdfDoc.getPage(i + 1);
const textContent = await pdfPage.getTextContent(textOptions);
const strBuf = [];
for (const textItem of textContent.items) {
strBuf.push(textItem.str);
if (textItem.hasEOL) {
strBuf.push("\n");
}
}
// Store the normalized page content (text items) as one string.
[
this._pageContents[i],
this._pageDiffs[i],
this._hasDiacritics[i],
] = normalize(strBuf.join(""));
resolve();
},
reason => {
console.error(
`Unable to get text content for page ${i + 1}`,
reason
);
// Page error -- assuming no text content.
this._pageContents[i] = "";
this._pageDiffs[i] = null;
this._hasDiacritics[i] = false;
resolve();
for (const textItem of textContent.items) {
strBuf.push(textItem.str);
if (textItem.hasEOL) {
strBuf.push("\n");
}
);
}
// Store the normalized page content (text items) as one string.
[this._pageContents[i], this._pageDiffs[i], this._hasDiacritics[i]] =
normalize(strBuf.join(""));
} catch (ex) {
console.error(`Unable to get text content for page ${i + 1}`, ex);
// Page error -- assuming no text content.
[this._pageContents[i], this._pageDiffs[i], this._hasDiacritics[i]] =
["", null, false];
}
resolve();
});
}
}