我可以从服务器向客户端GRPC发送自定义错误消息吗? [英] Can I send a custom Error message from server to client GRPC?

查看:650
本文介绍了我可以从服务器向客户端GRPC发送自定义错误消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的GRPC服务器和客户端.

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:

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();

和我的原始文件

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

服务器.

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

客户端.

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

因此,我创建的自定义javascript错误的newStatus, newMessage属性已删除,并且已转换为GRPC标准错误消息.

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

我的问题是

  1. 是否可以向客户发送自定义消息?
  2. 我可以创建GRPC错误,而不是javascript错误吗?
  3. 将自定义属性发送到客户端的一种方法是我认为是将自定义数据添加到Metadata.但我也不知道该怎么做.
  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 .

推荐答案

在gRPC Google网上论坛,对于相同的问题有一个有用的答复: https://groups.google.com/d/msg/grpc-io /X_bUx3T8S7s/x38FU429CgAJ

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

您可以使用错误将自定义状态消息发送给客户端 对象的消息属性.在您的示例中,这是"MY_ERROR".这 状态代码应在"code"属性中,就像您看到的一样 在客户端.

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.

如果要使用gRPC状态结构而不是JavaScript 错误,您可以通过填充"code"属性和 对象的消息"或详细信息"属性.

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.

如果您要发送元数据,则应构造一个实例 grpc.Metadata类,然后将键/值对添加到结果对象. 然后,您可以将其作为回调的第三个参数传递或设置 错误的元数据"属性,将其与错误一起发送给客户端.

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.

请注意,gRPC使用的状态代码不是HTTP状态 代码,但在grpc.status中定义的gRPC特定代码.你 应该只使用这些代码设置错误的代码属性.如果你 要发送自己的代码,请改用元数据.

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.

要发送带有错误的自定义消息,请使用该消息构造一个Error.设置message属性:

To send a custom message with the error, construct an Error with the message. This sets the message property:

var jsErr = new Error('Unauthorized');

如上所述,在您的情况下直接设置gRPC状态代码可能没有用.但是,作为参考,可以通过错误的code属性设置gRPC状态代码:

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);

然后客户端输出为:

[ 'stack', 'message', 'code', 'metadata' ]
{ [Error: Unauthorized]
  code: 7,
  metadata: Metadata { _internal_repr: { key1: [Object], key2: [Object] } } }
Metadata { _internal_repr: { key1: [ 'value2' ], key2: [ 'value2' ] } }

这篇关于我可以从服务器向客户端GRPC发送自定义错误消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆