Ensure that the WorkerTransport._passwordCapability is always rejected, even when errors are thrown in PDFDocumentLoadingTask.onPassword callback

Please note that while the current code works, both in the viewer and the unit-tests, it can leave the `WorkerTransport._passwordCapability` Promise in a pending state.
In the `PasswordRequest` handler, in src/display/api.js, we're returning the Promise from a `capability` object (rather than just a "plain" Promise). While an error thrown anywhere within this handler was fortunately enough to propagate it to the Worker side, it won't cause the Promise (in `WorkerTransport._passwordCapability`) to actually be rejected.
Finally note that while we're now catching errors in the `PasswordRequest` handler, those errors are still propagated to the Worker side via the (now) rejected Promise and the existing `return this._passwordCapability.promise;` line.

This prevents warnings about uncaught Promises, with messages such as "Error: Worker was destroyed during onPassword callback", when running the unit-tests both in browsers *and* in Node.js/Travis.
This commit is contained in:
Jonas Jenwald 2018-06-01 12:52:47 +02:00
parent 0ecc22cb04
commit ef081a0531
2 changed files with 7 additions and 3 deletions

View File

@ -574,9 +574,9 @@ var WorkerMessageHandler = {
finishWorkerTask(task);
pdfManager.updatePassword(data.password);
pdfManagerReady();
}).catch(function (ex) {
}).catch(function (boundException) {
finishWorkerTask(task);
handler.send('PasswordException', ex);
handler.send('PasswordException', boundException);
}.bind(null, e));
} else if (e instanceof InvalidPDFException) {
handler.send('InvalidPDF', e);

View File

@ -1788,7 +1788,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
password,
});
};
loadingTask.onPassword(updatePassword, exception.code);
try {
loadingTask.onPassword(updatePassword, exception.code);
} catch (ex) {
this._passwordCapability.reject(ex);
}
} else {
this._passwordCapability.reject(
new PasswordException(exception.message, exception.code));