I have created a simple GRPC server and client .
What i want to do is to create a custom error in the server and pass it to the client. My code looks as follows:<
There is a helpful reply to this same question on the gRPC Google Group: https://groups.google.com/d/msg/grpc-io/X_bUx3T8S7s/x38FU429CgAJ
You can send a custom status message to the client using the Error object's message property. In your example, that is "MY_ERROR". The status code should be in the "code" property, just like how you see it on the client side.
If you want to use the gRPC status structure instead of a JavaScript error, you can do so by populating the "code" property and the "message" or "details" property of the object.
If you want to send metadata, you should construct an instance of the grpc.Metadata class, then add key/value pairs to the resulting object. Then you can pass it as the third argument of the callback or set the error's "metadata" property to send it to the client with the error.
Please note that the status codes that gRPC uses are not HTTP status codes, but gRPC specific codes that are defined in grpc.status. You should only set the error's code property using those codes. If you want to send your own codes, use metadata instead.
I'll illustrate what's written above with some examples.
To send a custom message with the error, construct an Error
with the message. This sets the message
property:
var jsErr = new Error('Unauthorized');
As mentioned above, it's probably not useful to directly set gRPC status codes in your case. But, for reference, the gRPC status code can be set through the error's code
property:
jsErr.code = grpc.status.PERMISSION_DENIED;
To send your own error codes, or other information, use metadata:
var metadata = new grpc.Metadata();
metadata.set('key1', 'value2');
metadata.set('key2', 'value2');
jsErr.metadata = metadata;
Now, if the server constructs the error as above and the client outputs the returned error with:
console.log(Object.getOwnPropertyNames(err));
console.log(err);
console.log(err.metadata);
then the client output is:
[ 'stack', 'message', 'code', 'metadata' ]
{ [Error: Unauthorized]
code: 7,
metadata: Metadata { _internal_repr: { key1: [Object], key2: [Object] } } }
Metadata { _internal_repr: { key1: [ 'value2' ], key2: [ 'value2' ] } }
1.Yes 2.Maybe
Avoid sending special objects (like new Error
) over the wire. Send simple object with an error property and look for its value on the other end. See http://json.org/ to have an overview of easy transferable data.
inside Server.js try
function sayHello(call, callback) {
try {
var myCustomError = {};
myCustomError.newStatus = 401;
myCustomError.newMessage = 'custom unAuthorized error';
console.log(Object.getOwnPropertyNames(myCustomError ));
console.log(myCustomError);
callback(null, {error: myCustomError, message: ""});
} catch(e) {
callback(e);
}
}
inside the Client.js
client.sayHello({name: user}, function(err, response) {
var myCustomError= response.error;
if (myCustomError) {
console.log(Object.getOwnPropertyNames(myCustomError));
console.log(myCustomError);
}
});