Move the error message localization into PDFViewerApplication._otherError

When reporting errors in the viewer we currently localize the error messages "manually" at every call-site, which seems like unnecessary repetition.
This commit is contained in:
Jonas Jenwald 2024-03-01 13:40:15 +01:00
parent 0022310b9c
commit b5e00e1fae
2 changed files with 27 additions and 26 deletions

View File

@ -838,9 +838,7 @@ const PDFViewerApplication = {
this.open({ url, length, originalUrl }); this.open({ url, length, originalUrl });
}, },
onError: err => { onError: err => {
this.l10n.get("pdfjs-loading-error").then(msg => { this._documentError("pdfjs-loading-error", err);
this._documentError(msg, err);
});
}, },
onProgress: (loaded, total) => { onProgress: (loaded, total) => {
this.progress(loaded / total); this.progress(loaded / total);
@ -1050,10 +1048,11 @@ const PDFViewerApplication = {
} else if (reason instanceof UnexpectedResponseException) { } else if (reason instanceof UnexpectedResponseException) {
key = "pdfjs-unexpected-response-error"; key = "pdfjs-unexpected-response-error";
} }
return this.l10n.get(key).then(msg => { return this._documentError(key, { message: reason.message }).then(
this._documentError(msg, { message: reason?.message }); () => {
throw reason; throw reason;
}); }
);
} }
); );
}, },
@ -1134,10 +1133,13 @@ const PDFViewerApplication = {
* Report the error; used for errors affecting loading and/or parsing of * Report the error; used for errors affecting loading and/or parsing of
* the entire PDF document. * the entire PDF document.
*/ */
_documentError(message, moreInfo = null) { async _documentError(key, moreInfo = null) {
this._unblockDocumentLoadEvent(); this._unblockDocumentLoadEvent();
this._otherError(message, moreInfo); const message = await this._otherError(
key || "pdfjs-loading-error",
moreInfo
);
this.eventBus.dispatch("documenterror", { this.eventBus.dispatch("documenterror", {
source: this, source: this,
@ -1148,12 +1150,15 @@ const PDFViewerApplication = {
/** /**
* Report the error; used for errors affecting e.g. only a single page. * Report the error; used for errors affecting e.g. only a single page.
* @param {string} message - A message that is human readable. * @param {string} key - The localization key for the error.
* @param {Object} [moreInfo] - Further information about the error that is * @param {Object} [moreInfo] - Further information about the error that is
* more technical. Should have a 'message' and * more technical. Should have a 'message' and
* optionally a 'stack' property. * optionally a 'stack' property.
* @returns {string} A (localized) error message that is human readable.
*/ */
_otherError(message, moreInfo = null) { async _otherError(key, moreInfo = null) {
const message = await this.l10n.get(key);
const moreInfoText = [`PDF.js v${version || "?"} (build: ${build || "?"})`]; const moreInfoText = [`PDF.js v${version || "?"} (build: ${build || "?"})`];
if (moreInfo) { if (moreInfo) {
moreInfoText.push(`Message: ${moreInfo.message}`); moreInfoText.push(`Message: ${moreInfo.message}`);
@ -1171,6 +1176,7 @@ const PDFViewerApplication = {
} }
console.error(`${message}\n\n${moreInfoText.join("\n")}`); console.error(`${message}\n\n${moreInfoText.join("\n")}`);
return message;
}, },
progress(level) { progress(level) {
@ -1387,9 +1393,7 @@ const PDFViewerApplication = {
this._initializeAutoPrint(pdfDocument, openActionPromise); this._initializeAutoPrint(pdfDocument, openActionPromise);
}, },
reason => { reason => {
this.l10n.get("pdfjs-loading-error").then(msg => { this._documentError("pdfjs-loading-error", { message: reason.message });
this._documentError(msg, { message: reason?.message });
});
} }
); );
@ -1778,9 +1782,7 @@ const PDFViewerApplication = {
} }
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
this.l10n.get("pdfjs-printing-not-supported").then(msg => { this._otherError("pdfjs-printing-not-supported");
this._otherError(msg);
});
return; return;
} }
@ -2242,8 +2244,8 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's"); throw new Error("file origin does not match viewer's");
} }
} catch (ex) { } catch (ex) {
PDFViewerApplication.l10n.get("pdfjs-loading-error").then(msg => { PDFViewerApplication._documentError("pdfjs-loading-error", {
PDFViewerApplication._documentError(msg, { message: ex?.message }); message: ex.message,
}); });
throw ex; throw ex;
} }
@ -2308,9 +2310,7 @@ function webViewerPageRendered({ pageNumber, error }) {
} }
if (error) { if (error) {
PDFViewerApplication.l10n.get("pdfjs-rendering-error").then(msg => { PDFViewerApplication._otherError("pdfjs-rendering-error", error);
PDFViewerApplication._otherError(msg, error);
});
} }
} }

View File

@ -108,10 +108,11 @@ const ChromeCom = {
// Even without this check, the file load in frames is still blocked, // Even without this check, the file load in frames is still blocked,
// but this may change in the future (https://crbug.com/550151). // but this may change in the future (https://crbug.com/550151).
if (origin && !/^file:|^chrome-extension:/.test(origin)) { if (origin && !/^file:|^chrome-extension:/.test(origin)) {
viewerApp._documentError( viewerApp._documentError(null, {
`Blocked ${origin} from loading ${file}. Refused to load ` + message:
"a local file in a non-local page for security reasons." `Blocked ${origin} from loading ${file}. Refused to load ` +
); "a local file in a non-local page for security reasons.",
});
return; return;
} }
isAllowedFileSchemeAccess(function (isAllowedAccess) { isAllowedFileSchemeAccess(function (isAllowedAccess) {