Make event order consistent, and make 'end' and 'error' mutually exclusive

This commit is contained in:
murgatroid99 2016-12-13 18:05:33 -08:00
parent 0ea53cccdd
commit 6962aed6fa
2 changed files with 12 additions and 9 deletions

View File

@ -184,14 +184,15 @@ function _emitStatusIfDone() {
} else {
status = this.received_status;
}
this.emit('status', status);
if (status.code !== grpc.status.OK) {
if (status.code === grpc.status.OK) {
this.push(null);
} else {
var error = new Error(status.details);
error.code = status.code;
error.metadata = status.metadata;
this.emit('error', error);
return;
}
this.emit('status', status);
}
}
@ -224,9 +225,11 @@ function _read(size) {
} catch (e) {
self._readsDone({code: grpc.status.INTERNAL,
details: 'Failed to parse server response'});
return;
}
if (data === null) {
self._readsDone();
return;
}
if (self.push(deserialized) && data !== null) {
var read_batch = {};
@ -396,6 +399,8 @@ function makeUnaryRequestFunction(method, serialize, deserialize) {
var status = response.status;
var error;
var deserialized;
emitter.emit('metadata', Metadata._fromCoreRepresentation(
response.metadata));
if (status.code === grpc.status.OK) {
if (err) {
// Got a batch error, but OK status. Something went wrong
@ -423,8 +428,6 @@ function makeUnaryRequestFunction(method, serialize, deserialize) {
args.callback(null, deserialized);
}
emitter.emit('status', status);
emitter.emit('metadata', Metadata._fromCoreRepresentation(
response.metadata));
});
return emitter;
}

View File

@ -179,8 +179,8 @@ describe('Server.prototype.addProtoService', function() {
call.on('data', function(value) {
assert.fail('No messages expected');
});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.UNIMPLEMENTED);
call.on('error', function(err) {
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
done();
});
});
@ -189,8 +189,8 @@ describe('Server.prototype.addProtoService', function() {
call.on('data', function(value) {
assert.fail('No messages expected');
});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.UNIMPLEMENTED);
call.on('error', function(err) {
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
done();
});
call.end();