Merge pull request #14345 from Snuffleupagus/viewer-pagesPromise-reject

Ensure that the viewer handles `BaseViewer` initialization failures
This commit is contained in:
Tim van der Meij 2021-12-05 13:40:24 +01:00 committed by GitHub
commit 3264d72e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 19 deletions

View File

@ -949,22 +949,22 @@ const PDFViewerApplication = {
pdfDocument => { pdfDocument => {
this.load(pdfDocument); this.load(pdfDocument);
}, },
exception => { reason => {
if (loadingTask !== this.pdfLoadingTask) { if (loadingTask !== this.pdfLoadingTask) {
return undefined; // Ignore errors for previously opened PDF files. return undefined; // Ignore errors for previously opened PDF files.
} }
let key = "loading_error"; let key = "loading_error";
if (exception instanceof InvalidPDFException) { if (reason instanceof InvalidPDFException) {
key = "invalid_file_error"; key = "invalid_file_error";
} else if (exception instanceof MissingPDFException) { } else if (reason instanceof MissingPDFException) {
key = "missing_file_error"; key = "missing_file_error";
} else if (exception instanceof UnexpectedResponseException) { } else if (reason instanceof UnexpectedResponseException) {
key = "unexpected_response_error"; key = "unexpected_response_error";
} }
return this.l10n.get(key).then(msg => { return this.l10n.get(key).then(msg => {
this._documentError(msg, { message: exception?.message }); this._documentError(msg, { message: reason?.message });
throw exception; throw reason;
}); });
} }
); );
@ -1368,11 +1368,18 @@ const PDFViewerApplication = {
}); });
}); });
pagesPromise.then(() => { pagesPromise.then(
this._unblockDocumentLoadEvent(); () => {
this._unblockDocumentLoadEvent();
this._initializeAutoPrint(pdfDocument, openActionPromise); this._initializeAutoPrint(pdfDocument, openActionPromise);
}); },
reason => {
this.l10n.get("loading_error").then(msg => {
this._documentError(msg, { message: reason?.message });
});
}
);
onePageRendered.then(data => { onePageRendered.then(data => {
this.externalServices.reportTelemetry({ this.externalServices.reportTelemetry({

View File

@ -530,12 +530,14 @@ class BaseViewer {
this.eventBus.dispatch("scrollmodechanged", { source: this, mode }); this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
} }
this._pagesCapability.promise.then(() => { this._pagesCapability.promise.then(
this.eventBus.dispatch("pagesloaded", { () => {
source: this, this.eventBus.dispatch("pagesloaded", { source: this, pagesCount });
pagesCount, },
}); () => {
}); /* Prevent "Uncaught (in promise)"-messages in the console. */
}
);
this._onBeforeDraw = evt => { this._onBeforeDraw = evt => {
const pageView = this._pages[evt.pageNumber - 1]; const pageView = this._pages[evt.pageNumber - 1];
@ -680,6 +682,8 @@ class BaseViewer {
}) })
.catch(reason => { .catch(reason => {
console.error("Unable to initialize viewer", reason); console.error("Unable to initialize viewer", reason);
this._pagesCapability.reject(reason);
}); });
} }

View File

@ -72,7 +72,7 @@ class PDFOutlineViewer extends BaseTreeViewer {
this._pageNumberToDestHashCapability = null; this._pageNumberToDestHashCapability = null;
this._currentPageNumber = 1; this._currentPageNumber = 1;
this._isPagesLoaded = false; this._isPagesLoaded = null;
if ( if (
this._currentOutlineItemCapability && this._currentOutlineItemCapability &&
@ -93,8 +93,10 @@ class PDFOutlineViewer extends BaseTreeViewer {
this._pdfDocument?.loadingParams.disableAutoFetch this._pdfDocument?.loadingParams.disableAutoFetch
) { ) {
this._currentOutlineItemCapability.resolve(/* enabled = */ false); this._currentOutlineItemCapability.resolve(/* enabled = */ false);
} else if (this._isPagesLoaded) { } else if (this._isPagesLoaded !== null) {
this._currentOutlineItemCapability.resolve(/* enabled = */ true); this._currentOutlineItemCapability.resolve(
/* enabled = */ this._isPagesLoaded
);
} }
this.eventBus.dispatch("outlineloaded", { this.eventBus.dispatch("outlineloaded", {