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) => {
|
||||
assert(reason instanceof Error, 'cancel must have a valid reason');
|
||||
let cancelCapability = createPromiseCapability();
|
||||
this.streamControllers[streamId].cancelCall = cancelCapability;
|
||||
this.streamControllers[streamId].isClosed = true;
|
||||
@ -231,7 +232,7 @@ MessageHandler.prototype = {
|
||||
targetName,
|
||||
stream: StreamKind.CANCEL,
|
||||
streamId,
|
||||
reason,
|
||||
reason: wrapReason(reason),
|
||||
});
|
||||
// Return Promise to signal success or failure.
|
||||
return cancelCapability.promise;
|
||||
@ -287,6 +288,7 @@ MessageHandler.prototype = {
|
||||
},
|
||||
|
||||
error(reason) {
|
||||
assert(reason instanceof Error, 'error must have a valid reason');
|
||||
if (this.isCancelled) {
|
||||
return;
|
||||
}
|
||||
@ -296,7 +298,7 @@ MessageHandler.prototype = {
|
||||
targetName,
|
||||
stream: StreamKind.ERROR,
|
||||
streamId,
|
||||
reason,
|
||||
reason: wrapReason(reason),
|
||||
});
|
||||
},
|
||||
|
||||
@ -327,7 +329,7 @@ MessageHandler.prototype = {
|
||||
targetName,
|
||||
stream: StreamKind.START_COMPLETE,
|
||||
streamId,
|
||||
reason,
|
||||
reason: wrapReason(reason),
|
||||
});
|
||||
});
|
||||
},
|
||||
@ -397,7 +399,7 @@ MessageHandler.prototype = {
|
||||
targetName,
|
||||
stream: StreamKind.PULL_COMPLETE,
|
||||
streamId,
|
||||
reason,
|
||||
reason: wrapReason(reason),
|
||||
});
|
||||
});
|
||||
break;
|
||||
@ -450,7 +452,7 @@ MessageHandler.prototype = {
|
||||
targetName,
|
||||
stream: StreamKind.CANCEL_COMPLETE,
|
||||
streamId,
|
||||
reason,
|
||||
reason: wrapReason(reason),
|
||||
});
|
||||
});
|
||||
this.streamSinks[data.streamId].sinkCapability.
|
||||
|
@ -13,7 +13,9 @@
|
||||
* 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 { MessageHandler } from '../../src/shared/message_handler';
|
||||
|
||||
@ -142,12 +144,14 @@ describe('message_handler', function () {
|
||||
sink.onCancel = function (reason) {
|
||||
log += 'c';
|
||||
};
|
||||
log += '0';
|
||||
sink.ready.then(() => {
|
||||
log += '1';
|
||||
sink.enqueue([1, 2, 3, 4], 4);
|
||||
return sink.ready;
|
||||
}).then(() => {
|
||||
log += 'error';
|
||||
sink.error('error');
|
||||
log += 'e';
|
||||
sink.error(new Error('should not read when errored'));
|
||||
});
|
||||
});
|
||||
let messageHandler1 = new MessageHandler('main', 'worker', port);
|
||||
@ -161,15 +165,16 @@ describe('message_handler', function () {
|
||||
let reader = readable.getReader();
|
||||
|
||||
sleep(10).then(() => {
|
||||
expect(log).toEqual('');
|
||||
expect(log).toEqual('01');
|
||||
return reader.read();
|
||||
}).then((result) => {
|
||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||
expect(result.done).toEqual(false);
|
||||
return reader.read();
|
||||
}).then(() => {
|
||||
}, (reason) => {
|
||||
expect(reason).toEqual('error');
|
||||
}).catch((reason) => {
|
||||
expect(log).toEqual('01pe');
|
||||
expect(reason instanceof UnknownErrorException).toEqual(true);
|
||||
expect(reason.message).toEqual('should not read when errored');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user