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 3 commits into from Jan 12, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+26 −16
Diff settings

Always

Just for now

@@ -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
@@ -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);
@@ -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);
@@ -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);
Copy path View file
@@ -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);
@@ -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();
Copy path View file
@@ -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});
@@ -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();
});
});
@@ -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();
});
});
@@ -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();
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.