Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return grpc error in nodejs

Tags:

node.js

grpc

I want to return grpc error code and description in server-side. I have tried this

function sayHello(call, callback) {
  callback({error: {code: 400, message: "invalid input"});
}

but I got this exception from client

{ Error: Unknown Error
    at /home/thanh/email/node_modules/grpc/src/node/src/client.js:434:17 code: 2, metadata: Metadata { _internal_repr: {} } }

If I don't want to include error field in message definition like this.

message Hello {
  string name = 1;
  string error = 2; // don't want this
}

Then what is the proper way to send grpc error back to client ?

like image 763
thanhpk Avatar asked Mar 16 '17 09:03

thanhpk


1 Answers

Change it to:

return callback({
  code: 400,
  message: "invalid input",
  status: grpc.status.INTERNAL
})
like image 107
avi Avatar answered Sep 17 '22 00:09

avi