Fix the exception propagation when rejecting workerReadyCapability

Currently when an exception is thrown, we try to reject `workerReadyCapability` with multiple arguments in src/core/api.js. This obviously doesn't work, hence this patch changes that to instead reject with the exception object as is.
In src/core/worker.js the exception is currently (unncessarily) wrapped in an object, so this patch also simplifies that to directly send the exception object instead.
This commit is contained in:
Jonas Jenwald 2014-08-23 16:03:49 +02:00
parent b3be74d81c
commit ca027ebfdb
3 changed files with 40 additions and 40 deletions

View File

@ -144,7 +144,7 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
if (status === 404) { if (status === 404) {
var exception = new MissingPDFException('Missing PDF "' + var exception = new MissingPDFException('Missing PDF "' +
source.url + '".'); source.url + '".');
handler.send('MissingPDF', { exception: exception }); handler.send('MissingPDF', exception);
} else { } else {
handler.send('DocError', 'Unexpected server response (' + handler.send('DocError', 'Unexpected server response (' +
status + ') while retrieving PDF "' + status + ') while retrieving PDF "' +
@ -200,26 +200,17 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
var onFailure = function(e) { var onFailure = function(e) {
if (e instanceof PasswordException) { if (e instanceof PasswordException) {
if (e.code === PasswordResponses.NEED_PASSWORD) { if (e.code === PasswordResponses.NEED_PASSWORD) {
handler.send('NeedPassword', { handler.send('NeedPassword', e);
exception: e
});
} else if (e.code === PasswordResponses.INCORRECT_PASSWORD) { } else if (e.code === PasswordResponses.INCORRECT_PASSWORD) {
handler.send('IncorrectPassword', { handler.send('IncorrectPassword', e);
exception: e
});
} }
} else if (e instanceof InvalidPDFException) { } else if (e instanceof InvalidPDFException) {
handler.send('InvalidPDF', { handler.send('InvalidPDF', e);
exception: e
});
} else if (e instanceof MissingPDFException) { } else if (e instanceof MissingPDFException) {
handler.send('MissingPDF', { handler.send('MissingPDF', e);
exception: e
});
} else { } else {
handler.send('UnknownError', { handler.send('UnknownError',
exception: new UnknownErrorException(e.message, e.toString()) new UnknownErrorException(e.message, e.toString()));
});
} }
}; };

View File

@ -14,11 +14,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals CanvasGraphics, combineUrl, createScratchCanvas, error, /* globals PDFJS, isArrayBuffer, error, combineUrl, createPromiseCapability,
FontLoader, globalScope, info, isArrayBuffer, loadJpegStream, StatTimer, globalScope, MessageHandler, info, FontLoader, Util, warn,
MessageHandler, PDFJS, Promise, StatTimer, warn, PasswordResponses, PasswordException, InvalidPDFException,
PasswordResponses, Util, loadScript, createPromiseCapability, MissingPDFException, UnknownErrorException, FontFace, loadJpegStream,
FontFace */ createScratchCanvas, Promise, CanvasGraphics */
'use strict'; 'use strict';
@ -865,36 +865,40 @@ var WorkerTransport = (function WorkerTransportClosure() {
this.workerReadyCapability.resolve(pdfDocument); this.workerReadyCapability.resolve(pdfDocument);
}, this); }, this);
messageHandler.on('NeedPassword', function transportPassword(data) { messageHandler.on('NeedPassword',
function transportNeedPassword(exception) {
if (this.passwordCallback) { if (this.passwordCallback) {
return this.passwordCallback(updatePassword, return this.passwordCallback(updatePassword,
PasswordResponses.NEED_PASSWORD); PasswordResponses.NEED_PASSWORD);
} }
this.workerReadyCapability.reject(data.exception.message, this.workerReadyCapability.reject(
data.exception); new PasswordException(exception.message, exception.code));
}, this); }, this);
messageHandler.on('IncorrectPassword', function transportBadPass(data) { messageHandler.on('IncorrectPassword',
function transportIncorrectPassword(exception) {
if (this.passwordCallback) { if (this.passwordCallback) {
return this.passwordCallback(updatePassword, return this.passwordCallback(updatePassword,
PasswordResponses.INCORRECT_PASSWORD); PasswordResponses.INCORRECT_PASSWORD);
} }
this.workerReadyCapability.reject(data.exception.message, this.workerReadyCapability.reject(
data.exception); new PasswordException(exception.message, exception.code));
}, this); }, this);
messageHandler.on('InvalidPDF', function transportInvalidPDF(data) { messageHandler.on('InvalidPDF', function transportInvalidPDF(exception) {
this.workerReadyCapability.reject(data.exception.name, data.exception); this.workerReadyCapability.reject(
new InvalidPDFException(exception.message));
}, this); }, this);
messageHandler.on('MissingPDF', function transportMissingPDF(data) { messageHandler.on('MissingPDF', function transportMissingPDF(exception) {
this.workerReadyCapability.reject(data.exception.message, this.workerReadyCapability.reject(
data.exception); new MissingPDFException(exception.message));
}, this); }, this);
messageHandler.on('UnknownError', function transportUnknownError(data) { messageHandler.on('UnknownError',
this.workerReadyCapability.reject(data.exception.message, function transportUnknownError(exception) {
data.exception); this.workerReadyCapability.reject(
new UnknownErrorException(exception.message, exception.details));
}, this); }, this);
messageHandler.on('DataLoaded', function transportPage(data) { messageHandler.on('DataLoaded', function transportPage(data) {

View File

@ -672,11 +672,16 @@ var PDFView = {
self.load(pdfDocument, scale); self.load(pdfDocument, scale);
self.loading = false; self.loading = false;
}, },
function getDocumentError(message, exception) { function getDocumentError(exception) {
var name, message;
if (exception) {
name = exception.name;
message = exception.message;
}
var loadingErrorMessage = mozL10n.get('loading_error', null, var loadingErrorMessage = mozL10n.get('loading_error', null,
'An error occurred while loading the PDF.'); 'An error occurred while loading the PDF.');
if (exception && exception.name === 'InvalidPDFException') { if (name === 'InvalidPDFException') {
// change error message also for other builds // change error message also for other builds
loadingErrorMessage = mozL10n.get('invalid_file_error', null, loadingErrorMessage = mozL10n.get('invalid_file_error', null,
'Invalid or corrupted PDF file.'); 'Invalid or corrupted PDF file.');
@ -686,7 +691,7 @@ var PDFView = {
//#endif //#endif
} }
if (exception && exception.name === 'MissingPDFException') { if (name === 'MissingPDFException') {
// special message for missing PDF's // special message for missing PDF's
loadingErrorMessage = mozL10n.get('missing_file_error', null, loadingErrorMessage = mozL10n.get('missing_file_error', null,
'Missing PDF file.'); 'Missing PDF file.');