Ensure that the cancel/error methods on Streams are always called with valid reason arguments

This commit is contained in:
Jonas Jenwald 2019-09-02 13:28:19 +02:00
parent 02bdacef42
commit 74f5a59f43
2 changed files with 8 additions and 3 deletions

View File

@ -223,6 +223,7 @@ MessageHandler.prototype = {
}, },
cancel: (reason) => { cancel: (reason) => {
assert(reason instanceof Error, 'cancel must have a valid reason');
let cancelCapability = createPromiseCapability(); let cancelCapability = createPromiseCapability();
this.streamControllers[streamId].cancelCall = cancelCapability; this.streamControllers[streamId].cancelCall = cancelCapability;
this.streamControllers[streamId].isClosed = true; this.streamControllers[streamId].isClosed = true;
@ -287,6 +288,7 @@ MessageHandler.prototype = {
}, },
error(reason) { error(reason) {
assert(reason instanceof Error, 'error must have a valid reason');
if (this.isCancelled) { if (this.isCancelled) {
return; return;
} }

View File

@ -13,7 +13,9 @@
* limitations under the License. * limitations under the License.
*/ */
import { AbortException, createPromiseCapability } from '../../src/shared/util'; import {
AbortException, createPromiseCapability, UnknownErrorException
} from '../../src/shared/util';
import { LoopbackPort } from '../../src/display/api'; import { LoopbackPort } from '../../src/display/api';
import { MessageHandler } from '../../src/shared/message_handler'; import { MessageHandler } from '../../src/shared/message_handler';
@ -149,7 +151,7 @@ describe('message_handler', function () {
return sink.ready; return sink.ready;
}).then(() => { }).then(() => {
log += 'e'; log += 'e';
sink.error('error'); sink.error(new Error('should not read when errored'));
}); });
}); });
let messageHandler1 = new MessageHandler('main', 'worker', port); let messageHandler1 = new MessageHandler('main', 'worker', port);
@ -171,7 +173,8 @@ describe('message_handler', function () {
return reader.read(); return reader.read();
}).catch((reason) => { }).catch((reason) => {
expect(log).toEqual('01pe'); expect(log).toEqual('01pe');
expect(reason).toEqual('error'); expect(reason instanceof UnknownErrorException).toEqual(true);
expect(reason.message).toEqual('should not read when errored');
done(); done();
}); });
}); });