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
Changes from 1 commit
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
27 changes: 18 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,21 @@ 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} ${status.name}: ${status.details}`;
let error = new Error(message);
error.code = status.code;
error.details = status.details;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include error.metadata as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That is actually what I meant to write there. The details string is in the message. It probably doesn't need to be in a separate field too.

return error;
}

/**
* Initial response metadata sent by the server when it starts processing the
* call
Expand Down Expand Up @@ -252,9 +267,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 +564,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 +645,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