Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code name to status error messages #126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/grpc-native-core/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ var Duplex = stream.Duplex;
var util = require('util');
var version = require('../package.json').version;

/**
* Create an Error object from a status object
* @private
* @param {grpc~StatusObject} status The status object
* @return {Error} The resulting Error
*/
function createStatusError(status) {
let statusName = _.invert(constants.status)[status.code];
let message = `${status.code} ${statusName}: ${status.details}`;
let error = new Error(message);
error.code = status.code;
error.metadata = status.metadata;
error.details = status.details;
return error;
}

/**
* Initial response metadata sent by the server when it starts processing the
* call
Expand Down Expand Up @@ -252,9 +268,7 @@ function _emitStatusIfDone() {
if (status.code === constants.status.OK) {
this.push(null);
} else {
var error = new Error(status.details);
error.code = status.code;
error.metadata = status.metadata;
var error = createStatusError(status);
this.emit('error', error);
}
this.emit('status', status);
Expand Down Expand Up @@ -551,9 +565,7 @@ Client.prototype.makeUnaryRequest = function(method, serialize, deserialize,
}
}
if (status.code !== constants.status.OK) {
error = new Error(status.details);
error.code = status.code;
error.metadata = status.metadata;
error = new createStatusError(status);
args.callback(error);
} else {
args.callback(null, deserialized);
Expand Down Expand Up @@ -634,9 +646,7 @@ Client.prototype.makeClientStreamRequest = function(method, serialize,
}
}
if (status.code !== constants.status.OK) {
error = new Error(response.status.details);
error.code = status.code;
error.metadata = status.metadata;
error = createStatusError(status);
args.callback(error);
} else {
args.callback(null, deserialized);
Expand Down
4 changes: 2 additions & 2 deletions test/api/credentials_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('client credentials', function() {
client_options);
client.unary({}, function(err, data) {
assert(err);
assert.strictEqual(err.message,
assert.strictEqual(err.details,
'Getting metadata from plugin failed with error: ' +
'Authentication error');
assert.strictEqual(err.code, grpc.status.UNAUTHENTICATED);
Expand Down Expand Up @@ -369,7 +369,7 @@ describe('client credentials', function() {
client_options);
client.unary({}, function(err, data) {
assert(err);
assert.strictEqual(err.message,
assert.strictEqual(err.details,
'Getting metadata from plugin failed with error: ' +
'Authentication failure');
done();
Expand Down
8 changes: 4 additions & 4 deletions test/api/surface_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,15 +981,15 @@ describe('Other conditions', function() {
client.unary({error: true}, function(err, data) {
assert(err);
assert.strictEqual(err.code, grpc.status.UNKNOWN);
assert.strictEqual(err.message, 'Requested error');
assert.strictEqual(err.details, 'Requested error');
done();
});
});
it('for a client stream call', function(done) {
var call = client.clientStream(function(err, data) {
assert(err);
assert.strictEqual(err.code, grpc.status.UNKNOWN);
assert.strictEqual(err.message, 'Requested error');
assert.strictEqual(err.details, 'Requested error');
done();
});
call.write({error: false});
Expand All @@ -1001,7 +1001,7 @@ describe('Other conditions', function() {
call.on('data', function(){});
call.on('error', function(error) {
assert.strictEqual(error.code, grpc.status.UNKNOWN);
assert.strictEqual(error.message, 'Requested error');
assert.strictEqual(error.details, 'Requested error');
done();
});
});
Expand All @@ -1013,7 +1013,7 @@ describe('Other conditions', function() {
call.on('data', function(){});
call.on('error', function(error) {
assert.strictEqual(error.code, grpc.status.UNKNOWN);
assert.strictEqual(error.message, 'Requested error');
assert.strictEqual(error.details, 'Requested error');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/interop/interop_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function statusCodeAndMessage(client, done) {
client.unaryCall(arg, function(err, resp) {
assert(err);
assert.strictEqual(err.code, 2);
assert.strictEqual(err.message, 'test status message');
assert.strictEqual(err.details, 'test status message');
done();
});
var duplex = client.fullDuplexCall();
Expand Down