Merge pull request #11115 from Snuffleupagus/MessageHandler-postMessage-wrapReason
Ensure that `Error`s are handled correctly when using `postMessage` with Streams in `MessageHandler`
This commit is contained in:
commit
7e37eb42ad
@ -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;
|
||||||
@ -231,7 +232,7 @@ MessageHandler.prototype = {
|
|||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.CANCEL,
|
stream: StreamKind.CANCEL,
|
||||||
streamId,
|
streamId,
|
||||||
reason,
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
// Return Promise to signal success or failure.
|
// Return Promise to signal success or failure.
|
||||||
return cancelCapability.promise;
|
return cancelCapability.promise;
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
@ -296,7 +298,7 @@ MessageHandler.prototype = {
|
|||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.ERROR,
|
stream: StreamKind.ERROR,
|
||||||
streamId,
|
streamId,
|
||||||
reason,
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -327,7 +329,7 @@ MessageHandler.prototype = {
|
|||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.START_COMPLETE,
|
stream: StreamKind.START_COMPLETE,
|
||||||
streamId,
|
streamId,
|
||||||
reason,
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -397,7 +399,7 @@ MessageHandler.prototype = {
|
|||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.PULL_COMPLETE,
|
stream: StreamKind.PULL_COMPLETE,
|
||||||
streamId,
|
streamId,
|
||||||
reason,
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -450,7 +452,7 @@ MessageHandler.prototype = {
|
|||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.CANCEL_COMPLETE,
|
stream: StreamKind.CANCEL_COMPLETE,
|
||||||
streamId,
|
streamId,
|
||||||
reason,
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
this.streamSinks[data.streamId].sinkCapability.
|
this.streamSinks[data.streamId].sinkCapability.
|
||||||
|
@ -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';
|
||||||
|
|
||||||
@ -142,12 +144,14 @@ describe('message_handler', function () {
|
|||||||
sink.onCancel = function (reason) {
|
sink.onCancel = function (reason) {
|
||||||
log += 'c';
|
log += 'c';
|
||||||
};
|
};
|
||||||
|
log += '0';
|
||||||
sink.ready.then(() => {
|
sink.ready.then(() => {
|
||||||
|
log += '1';
|
||||||
sink.enqueue([1, 2, 3, 4], 4);
|
sink.enqueue([1, 2, 3, 4], 4);
|
||||||
return sink.ready;
|
return sink.ready;
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
log += 'error';
|
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);
|
||||||
@ -161,15 +165,16 @@ describe('message_handler', function () {
|
|||||||
let reader = readable.getReader();
|
let reader = readable.getReader();
|
||||||
|
|
||||||
sleep(10).then(() => {
|
sleep(10).then(() => {
|
||||||
expect(log).toEqual('');
|
expect(log).toEqual('01');
|
||||||
return reader.read();
|
return reader.read();
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
expect(result.done).toEqual(false);
|
expect(result.done).toEqual(false);
|
||||||
return reader.read();
|
return reader.read();
|
||||||
}).then(() => {
|
}).catch((reason) => {
|
||||||
}, (reason) => {
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user