From ecf95e552ffab723eafde2eadbbef77290a9e0d6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 26 Jul 2023 09:19:03 +0200 Subject: [PATCH 1/2] Simplify handling of any `ViewHistory` errors during document loading in the viewer We can use modern JavaScript features, in this case optional chaining, to (ever so slightly) simplify how `ViewHistory` errors are handled. Also, use arrow functions when handling a few other (very rare) errors during loading since that's a tiny bit shorter. --- web/app.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/web/app.js b/web/app.js index 355e36c48..c70e3ee96 100644 --- a/web/app.js +++ b/web/app.js @@ -1289,13 +1289,13 @@ const PDFViewerApplication = { // Since the `setInitialView` call below depends on this being resolved, // fetch it early to avoid delaying initial rendering of the PDF document. - const pageLayoutPromise = pdfDocument.getPageLayout().catch(function () { + const pageLayoutPromise = pdfDocument.getPageLayout().catch(() => { /* Avoid breaking initial rendering; ignoring errors. */ }); - const pageModePromise = pdfDocument.getPageMode().catch(function () { + const pageModePromise = pdfDocument.getPageMode().catch(() => { /* Avoid breaking initial rendering; ignoring errors. */ }); - const openActionPromise = pdfDocument.getOpenAction().catch(function () { + const openActionPromise = pdfDocument.getOpenAction().catch(() => { /* Avoid breaking initial rendering; ignoring errors. */ }); @@ -1336,7 +1336,6 @@ const PDFViewerApplication = { }) .catch(() => { /* Unable to read from storage; ignoring errors. */ - return Object.create(null); }); firstPagePromise.then(pdfPage => { @@ -1369,7 +1368,7 @@ const PDFViewerApplication = { let scrollMode = AppOptions.get("scrollModeOnLoad"); let spreadMode = AppOptions.get("spreadModeOnLoad"); - if (stored.page && viewOnLoad !== ViewOnLoad.INITIAL) { + if (stored?.page && viewOnLoad !== ViewOnLoad.INITIAL) { hash = `page=${stored.page}&zoom=${zoom || stored.zoom},` + `${stored.scrollLeft},${stored.scrollTop}`; From 2fbfd9517f52c6ec1a1964bf0c4f73d794f362e5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 26 Jul 2023 10:39:51 +0200 Subject: [PATCH 2/2] Remove the unneeded error-handling at the end of `PDFViewerApplication.run` This is quite old code, however the error-handling no longer seems necessary for a couple of reasons: - The `PDFViewerApplication.open` method is asynchronous, which means that it cannot throw a "raw" `Error` and the try-catch is not needed in that case. - None of the other affected methods should throw, and if they do that'd rather indicate an *implementation* error in the code. - Finally, and most importantly, with the `PDFViewerApplication.run` method now being asynchronous an (unlikely) `Error` thrown within it will lead to a rejected `Promise` and not affect execution of other code. --- web/app.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/web/app.js b/web/app.js index c70e3ee96..f4489e956 100644 --- a/web/app.js +++ b/web/app.js @@ -694,7 +694,7 @@ const PDFViewerApplication = { async run(config) { await this.initialize(config); - const { appConfig, eventBus, l10n } = this; + const { appConfig, eventBus } = this; let file; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { const queryString = document.location.search.substring(1); @@ -745,7 +745,7 @@ const PDFViewerApplication = { if (!this.supportsDocumentFonts) { AppOptions.set("disableFontFace", true); - l10n.get("web_fonts_disabled").then(msg => { + this.l10n.get("web_fonts_disabled").then(msg => { console.warn(msg); }); } @@ -775,22 +775,16 @@ const PDFViewerApplication = { true ); - try { - if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - if (file) { - this.open({ url: file }); - } else { - this._hideViewBookmark(); - } - } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { - this.initPassiveLoading(file); + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + if (file) { + this.open({ url: file }); } else { - throw new Error("Not implemented: run"); + this._hideViewBookmark(); } - } catch (reason) { - l10n.get("loading_error").then(msg => { - this._documentError(msg, reason); - }); + } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { + this.initPassiveLoading(file); + } else { + throw new Error("Not implemented: run"); } },