Adding new errorback and code to display error messages.

This commit is contained in:
Brendan Dahl 2011-11-28 16:55:09 -08:00
parent 59d9dfc014
commit d6925b13ba
6 changed files with 119 additions and 15 deletions

View File

@ -63,6 +63,10 @@ var Page = (function pagePage() {
};
this.xref = xref;
this.ref = ref;
this.ctx = null;
this.callback = null;
this.errorback = null;
}
constructor.prototype = {
@ -165,8 +169,10 @@ var Page = (function pagePage() {
try {
self.display(gfx, self.callback);
} catch (e) {
if (self.callback) self.callback(e.toString());
throw e;
if (self.errorback)
self.errorback(e);
else
throw e;
}
});
};
@ -303,9 +309,10 @@ var Page = (function pagePage() {
}
return links;
},
startRendering: function pageStartRendering(ctx, callback) {
startRendering: function pageStartRendering(ctx, callback, errorback) {
this.ctx = ctx;
this.callback = callback;
this.errorback = errorback;
this.startRenderingTime = Date.now();
this.pdf.startRendering(this);
@ -599,6 +606,14 @@ var PDFDoc = (function pdfDoc() {
}
}.bind(this));
messageHandler.on('page_error', function pdfDocError(data) {
var page = this.pageCache[data.pageNum];
if (page.errorback)
page.errorback(data.error)
else
throw data.error;
}, this);
setTimeout(function pdfDocFontReadySetTimeout() {
messageHandler.send('doc', this.data);
this.workerReadyPromise.resolve(true);

View File

@ -14,7 +14,6 @@ function MessageHandler(name, comObj) {
ah['console_error'] = [function ahConsoleError(data) {
console.error.apply(console, data);
}];
comObj.onmessage = function messageHandlerComObjOnMessage(event) {
var data = event.data;
if (data.action in ah) {
@ -67,7 +66,6 @@ var WorkerMessageHandler = {
handler.on('page_request', function wphSetupPageRequest(pageNum) {
pageNum = parseInt(pageNum);
var page = pdfDoc.getPage(pageNum);
// The following code does quite the same as
// Page.prototype.startRendering, but stops at one point and sends the
@ -77,9 +75,22 @@ var WorkerMessageHandler = {
var start = Date.now();
var dependency = [];
// Pre compile the pdf page and fetch the fonts/images.
var IRQueue = page.getIRQueue(handler, dependency);
try {
var page = pdfDoc.getPage(pageNum);
// Pre compile the pdf page and fetch the fonts/images.
var IRQueue = page.getIRQueue(handler, dependency);
} catch(e) {
// Turn the error into an obj that can be serialized
e = {
message: e.message,
stack: e.stack
};
handler.send('page_error', {
pageNum: pageNum,
error: e
});
return;
}
console.log('page=%d - getIRQueue: time=%dms, len=%d', pageNum,
Date.now() - start, IRQueue.fnArray.length);

View File

@ -162,9 +162,11 @@ function nextPage(task, loadError) {
page.startRendering(
ctx,
function nextPageStartRendering(e) {
snapshotCurrentPage(task, (!failure && e) ?
('render : ' + e) : failure);
function nextPageStartRendering() {
snapshotCurrentPage(task, false);
},
function errorNextPageStartRendering(e) {
snapshotCurrentPage(task, 'render : ' + e.message);
}
);
} catch (e) {

View File

@ -252,6 +252,37 @@ canvas {
display: none;
}
#errorWrapper {
background: none repeat scroll 0 0 #FF5555;
color: white;
left: 0;
position: fixed;
right: 0;
top: 30px;
z-index: 1000;
padding: 3px;
}
#errorMessageLeft {
float: left;
}
#errorMessageRight {
float: right;
}
#errorMoreInfo {
background-color: #FFFFFF;
color: black;
padding: 3px;
margin: 3px;
white-space: pre;
}
.clearBoth {
clear: both;
}
/* === Printed media overrides === */
@media print {
#sidebar {

View File

@ -97,6 +97,24 @@
<span id="info">--</span>
</div>
<div id="errorWrapper" hidden='true'>
<div id="errorMessageLeft">
<span id="errorMessage"></span>
<button id="errorShowMore" onclick="" oncontextmenu="return false;">
More Information
</button>
<button id="errorShowLess" onclick="" oncontextmenu="return false;" hidden='true'>
Less Information
</button>
</div>
<div id="errorMessageRight">
<button id="errorClose" oncontextmenu="return false;">
X
</button>
</div>
<div class="clearBoth"></div>
<div id="errorMoreInfo" hidden='true'></div>
</div>
<div id="sidebar">
<div id="sidebarBox">

View File

@ -181,9 +181,34 @@ var PDFView = {
return '';
},
error: function pdfViewError() {
var loadingIndicator = document.getElementById('loading');
loadingIndicator.innerHTML = 'Error';
error: function pdfViewError(message, error) {
var errorWrapper = document.getElementById('errorWrapper');
errorWrapper.removeAttribute('hidden');
var errorMessage = document.getElementById('errorMessage');
errorMessage.innerHTML = message;
if (error) {
var errorMoreInfo = document.getElementById('errorMoreInfo');
var moreInfoButton = document.getElementById('errorShowMore');
var lessInfoButton = document.getElementById('errorShowLess');
var closeButton = document.getElementById('errorClose');
moreInfoButton.onclick = function() {
errorMoreInfo.removeAttribute('hidden');
moreInfoButton.setAttribute('hidden', 'true');
lessInfoButton.removeAttribute('hidden');
};
lessInfoButton.onclick = function() {
errorMoreInfo.setAttribute('hidden', 'true');
moreInfoButton.removeAttribute('hidden');
lessInfoButton.setAttribute('hidden', 'true');
};
closeButton.onclick = function() {
errorWrapper.setAttribute('hidden', 'true');
};
moreInfoButton.removeAttribute('hidden');
errorMoreInfo.innerHTML = error.message + '\n' + error.stack;
}
},
progress: function pdfViewProgress(level) {
@ -487,7 +512,9 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
ctx.translate(-this.x * scale, -this.y * scale);
stats.begin = Date.now();
this.content.startRendering(ctx, this.updateStats);
this.content.startRendering(ctx, this.updateStats, function(e) {
PDFView.error('An error occured while rendering the page.', e);
});
setupLinks(this.content, this.scale);
div.setAttribute('data-loaded', true);