Skip to content

Commit

Permalink
Add metadata in gRPC error
Browse files Browse the repository at this point in the history
  • Loading branch information
at-ishikawa committed Oct 28, 2019
1 parent 6bd0db0 commit ff86da4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions javascript/net/grpc/web/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ Error.prototype.code;
/** @export {(string|undefined)} */
Error.prototype.message;

/** @export {{Metadata}} */
Error.prototype.metadata;

exports = Error;
6 changes: 4 additions & 2 deletions javascript/net/grpc/web/grpcwebclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ GrpcWebClientBase.prototype.rpcCall = function(
if (status.code != StatusCode.OK) {
callback({
code: status.code,
message: status.details
message: status.details,
metadata: status.metadata
}, null);
}
});
Expand All @@ -99,7 +100,8 @@ GrpcWebClientBase.prototype.rpcCall = function(
if (error.code != StatusCode.OK) {
callback({
code: error.code,
message: error.message
message: error.message,
metadata: error.metadata
}, null);
}
});
Expand Down
2 changes: 2 additions & 0 deletions javascript/net/grpc/web/grpcwebclientreadablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ const GrpcWebClientReadableStream = function(genericTransportInterface) {
self.onErrorCallback_({
code: Number(grpcStatusCode),
message: grpcStatusMessage,
metadata: responseHeaders
});
errorEmitted = true;
}
if (self.onStatusCallback_) {
self.onStatusCallback_(/** @type {!Status} */ ({
code: Number(grpcStatusCode),
details: grpcStatusMessage,
metadata: responseHeaders
}));
}
}
Expand Down
38 changes: 35 additions & 3 deletions packages/grpc-web/test/generated_code_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('grpc-web generated code (commonjs+grpcwebtext)', function() {
});
});

it('should receive error', function(done) {
it('should receive error from trailers', function(done) {
execSync(genCodeCmd);
const {EchoServiceClient} = require(genCodePath);
const {EchoRequest} = require(protoGenCodePath);
Expand All @@ -228,15 +228,47 @@ describe('grpc-web generated code (commonjs+grpcwebtext)', function() {
request.setMessage('aaa');
MockXMLHttpRequest.onSend = function(xhr) {
xhr.respond(200, {'Content-Type': 'application/grpc-web-text'},
// a trailer frame with content 'grpc-status:10'
'gAAAABBncnBjLXN0YXR1czoxMA0K');
// a trailer frame with content 'grpc-status:10,grpc-message:bbb,custom-header:ccc
// ZGRk is the base64 format for ddd
'gAAAADVncnBjLXN0YXR1czoxMA0KZ3JwYy1tZXNzYWdlOmJiYg0KY3VzdG9tLWhlYWRlcjpjY2MNCg==');
};
var call = echoService.echo(request, {'custom-header-1':'value1'},
function(err, response) {
assert.equal(10, err.code);
assert.equal('bbb', err.message);
assert.equal('ccc', err.metadata['custom-header']);
done();
});
});

it('should receive error from response headers', function(done) {
execSync(genCodeCmd);
const {EchoServiceClient} = require(genCodePath);
const {EchoRequest} = require(protoGenCodePath);
var echoService = new EchoServiceClient('MyHostname', null, null);
var request = new EchoRequest();
request.setMessage('aaa');
MockXMLHttpRequest.onSend = function(xhr) {
xhr.respond(200, {
'Content-Type': 'application/grpc-web-text',
'grpc-status': 10,
'grpc-message': 'bbb',
'custom-header': 'ccc',
}, '');
};
var count = 0;
var call = echoService.echo(request, {'custom-header-1':'value1'},
function(err, response) {
assert.equal(10, err.code);
assert.equal('bbb', err.message);
assert.equal('ccc', err.metadata['custom-header']);
count++;
// Called twice because of status and error callback
if (count === 2) {
done();
}
});
});
});

describe('grpc-web generated code (closure+grpcwebtext)', function() {
Expand Down

0 comments on commit ff86da4

Please sign in to comment.