In display/api.js
ensure that we always reject with an Error
in JpegDecode
, and adjust a couple of other rejection sites as well
In the case where the document was destroyed, we were rejecting the `Promise` in `JpegDecode` with a string instead of an `Error`. The patch also brings the wording more inline with other such rejections. Use the `isInt` utility function when validating the `pageNumber` parameter in `WorkerTransport_getPage`, to make it more obvious what's actually happening. There's also a couple more unit-tests added, to ensure that we always fail in the expected way. Finally, we can simplify the rejection handling in `WorkerTransport_getPageIndexByRef` somewhat. (Note that the only reason for using `catch` here is that since the promise is rejected on the worker side, the `reason` becomes a string instead of an `Error` which is why we "re-reject" on the display side.)
This commit is contained in:
parent
38c85039d1
commit
37998076c9
@ -49,6 +49,7 @@ var error = sharedUtil.error;
|
|||||||
var deprecated = sharedUtil.deprecated;
|
var deprecated = sharedUtil.deprecated;
|
||||||
var getVerbosityLevel = sharedUtil.getVerbosityLevel;
|
var getVerbosityLevel = sharedUtil.getVerbosityLevel;
|
||||||
var info = sharedUtil.info;
|
var info = sharedUtil.info;
|
||||||
|
var isInt = sharedUtil.isInt;
|
||||||
var isArrayBuffer = sharedUtil.isArrayBuffer;
|
var isArrayBuffer = sharedUtil.isArrayBuffer;
|
||||||
var isSameOrigin = sharedUtil.isSameOrigin;
|
var isSameOrigin = sharedUtil.isSameOrigin;
|
||||||
var loadJpegStream = sharedUtil.loadJpegStream;
|
var loadJpegStream = sharedUtil.loadJpegStream;
|
||||||
@ -1612,7 +1613,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||||||
|
|
||||||
messageHandler.on('JpegDecode', function(data) {
|
messageHandler.on('JpegDecode', function(data) {
|
||||||
if (this.destroyed) {
|
if (this.destroyed) {
|
||||||
return Promise.reject('Worker was terminated');
|
return Promise.reject(new Error('Worker was destroyed'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageUrl = data[0];
|
var imageUrl = data[0];
|
||||||
@ -1662,8 +1663,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getPage: function WorkerTransport_getPage(pageNumber, capability) {
|
getPage: function WorkerTransport_getPage(pageNumber, capability) {
|
||||||
if (pageNumber <= 0 || pageNumber > this.numPages ||
|
if (!isInt(pageNumber) || pageNumber <= 0 || pageNumber > this.numPages) {
|
||||||
(pageNumber|0) !== pageNumber) {
|
|
||||||
return Promise.reject(new Error('Invalid page request'));
|
return Promise.reject(new Error('Invalid page request'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1686,12 +1686,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
|
getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
|
||||||
return this.messageHandler.sendWithPromise('GetPageIndex', { ref: ref }).
|
return this.messageHandler.sendWithPromise('GetPageIndex', {
|
||||||
then(function (pageIndex) {
|
ref: ref,
|
||||||
return pageIndex;
|
}).catch(function (reason) {
|
||||||
}, function (reason) {
|
return Promise.reject(new Error(reason));
|
||||||
return Promise.reject(new Error(reason));
|
});
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getAnnotations: function WorkerTransport_getAnnotations(pageIndex, intent) {
|
getAnnotations: function WorkerTransport_getAnnotations(pageIndex, intent) {
|
||||||
|
@ -341,13 +341,32 @@ describe('api', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('gets non-existent page', function(done) {
|
it('gets non-existent page', function(done) {
|
||||||
var promise = doc.getPage(100);
|
var outOfRangePromise = doc.getPage(100);
|
||||||
promise.then(function () {
|
var nonIntegerPromise = doc.getPage(2.5);
|
||||||
done.fail('shall fail for non-existent page');
|
var nonNumberPromise = doc.getPage('1');
|
||||||
}, function(data) {
|
|
||||||
expect(data instanceof Error).toEqual(true);
|
outOfRangePromise = outOfRangePromise.then(function () {
|
||||||
done();
|
throw new Error('shall fail for out-of-range pageNumber parameter');
|
||||||
|
}, function (reason) {
|
||||||
|
expect(reason instanceof Error).toEqual(true);
|
||||||
});
|
});
|
||||||
|
nonIntegerPromise = nonIntegerPromise.then(function () {
|
||||||
|
throw new Error('shall fail for non-integer pageNumber parameter');
|
||||||
|
}, function (reason) {
|
||||||
|
expect(reason instanceof Error).toEqual(true);
|
||||||
|
});
|
||||||
|
nonNumberPromise = nonNumberPromise.then(function () {
|
||||||
|
throw new Error('shall fail for non-number pageNumber parameter');
|
||||||
|
}, function (reason) {
|
||||||
|
expect(reason instanceof Error).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
Promise.all([outOfRangePromise, nonIntegerPromise, nonNumberPromise]).
|
||||||
|
then(function () {
|
||||||
|
done();
|
||||||
|
}).catch(function (reason) {
|
||||||
|
done.fail(reason);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('gets page index', function(done) {
|
it('gets page index', function(done) {
|
||||||
// reference to second page
|
// reference to second page
|
||||||
@ -365,8 +384,8 @@ describe('api', function() {
|
|||||||
var promise = doc.getPageIndex(ref);
|
var promise = doc.getPageIndex(ref);
|
||||||
promise.then(function () {
|
promise.then(function () {
|
||||||
done.fail('shall fail for invalid page reference.');
|
done.fail('shall fail for invalid page reference.');
|
||||||
}, function (data) {
|
}).catch(function (reason) {
|
||||||
expect(data instanceof Error).toEqual(true);
|
expect(reason instanceof Error).toEqual(true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user