Skip to content

Commit

Permalink
More robust getPage() error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Aug 4, 2017
1 parent 0c95bc7 commit d0e9372
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ class PDFFindController {
// Store the pageContent as a string.
this.pageContents[i] = strBuf.join('');
extractTextCapability.resolve(i);
}, (reason) => {
console.error(`Unable to get page ${i + 1} text content`, reason);
// Page error -- assuming no text content.
this.pageContents[i] = '';
extractTextCapability.resolve(i);
});
});
}
Expand Down
7 changes: 7 additions & 0 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PDFPageView {
this.id = options.id;
this.renderingId = 'page' + this.id;

this.pdfPage = null;
this.pageLabel = null;
this.rotation = 0;
this.scale = options.scale || DEFAULT_SCALE;
Expand Down Expand Up @@ -110,6 +111,7 @@ class PDFPageView {
this.reset();
if (this.pdfPage) {
this.pdfPage.cleanup();
this.pdfPage = null;
}
}

Expand Down Expand Up @@ -341,6 +343,11 @@ class PDFPageView {
this.reset(); // Ensure that we reset all state to prevent issues.
}

if (!this.pdfPage) {
this.renderingState = RenderingStates.FINISHED;
return Promise.reject(new Error('Page is not loaded'));
}

this.renderingState = RenderingStates.RUNNING;

let pdfPage = this.pdfPage;
Expand Down
10 changes: 8 additions & 2 deletions web/pdf_thumbnail_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ class PDFThumbnailViewer {

this.pdfDocument = pdfDocument;
if (!pdfDocument) {
return Promise.resolve();
return;
}

return pdfDocument.getPage(1).then((firstPage) => {
pdfDocument.getPage(1).then((firstPage) => {
let pagesCount = pdfDocument.numPages;
let viewport = firstPage.getViewport(1.0);
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
Expand All @@ -151,6 +151,8 @@ class PDFThumbnailViewer {
});
this._thumbnails.push(thumbnail);
}
}).catch((reason) => {
console.error('Unable to initialize thumbnail viewer', reason);
});
}

Expand Down Expand Up @@ -205,6 +207,10 @@ class PDFThumbnailViewer {
thumbView.setPdfPage(pdfPage);
this._pagesRequests[pageNumber] = null;
return pdfPage;
}).catch((reason) => {
console.error('Unable to get page for thumb view', reason);
// Page error -- there is nothing can be done.
this._pagesRequests[pageNumber] = null;
});
this._pagesRequests[pageNumber] = promise;
return promise;
Expand Down
14 changes: 13 additions & 1 deletion web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class PDFViewer {

// Fetch a single page so we can get a viewport that will be the default
// viewport for all pages
return firstPagePromise.then((pdfPage) => {
firstPagePromise.then((pdfPage) => {
let scale = this.currentScale;
let viewport = pdfPage.getViewport(scale * CSS_UNITS);
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
Expand Down Expand Up @@ -387,6 +387,12 @@ class PDFViewer {
if (--getPagesLeft === 0) {
pagesCapability.resolve();
}
}, (reason) => {
console.error(`Unable to get page ${pageNum} to initialize viewer`,
reason);
if (--getPagesLeft === 0) {
pagesCapability.resolve();
}
});
}
});
Expand All @@ -400,6 +406,8 @@ class PDFViewer {
if (this.findController) {
this.findController.resolveFirstPage();
}
}).catch((reason) => {
console.error('Unable to initialize viewer', reason);
});
}

Expand Down Expand Up @@ -838,6 +846,10 @@ class PDFViewer {
}
this._pagesRequests[pageNumber] = null;
return pdfPage;
}).catch((reason) => {
console.error('Unable to get page for page view', reason);
// Page error -- there is nothing can be done.
this._pagesRequests[pageNumber] = null;
});
this._pagesRequests[pageNumber] = promise;
return promise;
Expand Down

0 comments on commit d0e9372

Please sign in to comment.