I am working with Node js and GRPC . i have created a simple GRPC server 
and client . 

What i want to do is create a custom error in the server and pass it to the 
client . my code looks like this .

*Server.js*

var error = require('error');
var PROTO_PATH = grpc.load(__dirname + '/proto/hello.proto');var hello_proto = 
PROTO_PATH.hello;
function sayHello(call, callback) {

    try {
        var jsErr = new Error('MY_ERROR');
        jsErr.newStatus = 401;
        jsErr.newMessage = 'custom unAuthorized error';
        console.log(Object.getOwnPropertyNames(jsErr));
        console.log(jsErr);
        callback(jsErr);

    } catch(e) {
        callback(e);
    }}
function sayHelloAgain(call, callback) {
    callback(null, {message: 'Hello Again ' + call.request.name});}
function main() {

    var server = new grpc.Server();
    server.addProtoService(hello_proto.Hello.service, {sayHello: 
sayHello,sayHelloAgain: sayHelloAgain });
    server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
    server.start();}

main();

*Client.js*

var grpc = require('grpc');
var PROTO_PATH = grpc.load(__dirname + '/proto/hello.proto');var hello_proto = 
PROTO_PATH.hello;
function main() {
    var client = new 
hello_proto.Hello('localhost:50051',grpc.credentials.createInsecure());
    var user;
    if (process.argv.length >= 3) {
        user = process.argv[2];
    } else {
        user = 'world';
    }

    client.sayHello({name: user}, function(err, response) {

        console.log(Object.getOwnPropertyNames(err));
        console.log(err);
    });}

main();

and my proto file 

syntax = "proto3";
package hello;

service Hello {
    rpc sayHello(sayHelloRequest) returns (sayHelloResponse) {}
    rpc sayHelloAgain(sayHelloRequest) returns (sayHelloResponse) {}}


message sayHelloRequest {
    string name = 1;}

message sayHelloResponse {
    string message = 1;}

when i run the cient the result from each looks like this 

Server .

[ 'stack', 'message', 'newStatus', 'newMessage' ]{ [Error: MY_ERROR] newStatus: 
401, newMessage: 'custom unAutorized error' }

Client .

[ 'stack', 'message', 'code', 'metadata' ]{ [Error: MY_ERROR] code: 2, 
metadata: Metadata { _internal_repr: {} } }

So my created custom javascript error's newStatus, newMessage properties 
have removed and it has converted to GRPC standard error message . 

My Questions are 

   1. Is it possible to send a custom message to client ?
   2. Can i create a GRPC error , not a javascript error ?
   3. one way to send custom attributes to client is i think is add the 
   custom data to Metadata . but i am also not sure how to do it . 

Please help . Thanks in advance .

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/0ea68f7d-c652-45e8-a10b-08100dd804c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to