M
M
Max Zhukov2019-12-20 19:17:36
JavaScript
Max Zhukov, 2019-12-20 19:17:36

How can I submit an error on client stream grpc nodejs?

How can I submit an error on stream grpc nodejs?
There is a server and a client. I want to send an error to the client stream and catch it in the on('error') function on the server.
channel.destroy(new Error("test")) produces the following message
5dfcf3bfe059f172085906.png
client

const client = new protoDescriptor.grpc.CarService('localhost:50051', grpc.credentials.createInsecure());
let channel = client.streamCars((err,response)=>{
    console.log(err,response);
});

let obj = {
    cars: []
}

let count = 0;
let intervalId = setInterval(()=>{
    count++;
    obj.cars.push({});
    channel.write(obj);
    if (count > 3){
        clearInterval(intervalId);
        channel.end();
    }
},1000);

server
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);

let streamCars = (call,callback) => {
    call.on('data', (response) => {
        console.log(response);
    });
    call.on('end', () => {
        console.log('end server');
        callback(null, {})
    });
    
    call.on('error', (response) => {
        console.log(response);
    });
    
    call.on('status', (response) => {
        console.log(response);
    });
}

let server = new grpc.Server();

server.addService(protoDescriptor.grpc.CarService.service, {
    streamCars
});

server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question