Search code examples
c++error-handlingruntime-errorprotocol-buffersgrpc

Display runtime error message in gRPC server side and pass it to the client


I am using gRPC in a project where I have to set and get values from some separate/outside functions. Some functions has case that if they get unwanted value they will throw runtime error. By following this I have got an idea to catch a error_state from inside of the gRPC function.

I am giving here some of my approach. A demo source is this

proto file where only including here the client message part

message NameQuery {
    string name = 1;
    int32 cl_value = 2; // This is a user input data which will be passed to the server and then to a outside function

client/src/main.cpp

int main(int argc, char* argv[])
{
    // Setup request
    expcmake::NameQuery query;
    expcmake::Address result;
    query.set_name("John");

    int x;
    cout << "give value of x: ";
    cin>> x;
    query.set_cl_value(x);

   // remaining are as like as before

server/src/main.cpp

#include <iostream>
using namespace std;

void Check_Value(const ::expcmake::NameQuery* request)
{
    if (request->cl_value() < 5)
        cout << "request->cl_value(): " << request->cl_value() << endl;
    else
        throw std::runtime_error("********** BAD VALUE **********");
}

class AddressBookService final : public expcmake::AddressBook::Service {
    public:
        virtual ::grpc::Status GetAddress(::grpc::ServerContext* context, const ::expcmake::NameQuery* request, ::expcmake::Address* response)
        {
            std::cout << "Server: GetAddress for \"" << request->name() << "\"." << std::endl;
            Check_Value(request);

        // remaining are as like as before

After building the project if from client side 5 or greater than 5 is given server didn't show any message but running continuously(which is obvious for the Wait function of gRPC) where my expectation was it should print in server console

********** BAD VALUE **********

Though, in client side I have got all passed value as BLANK where I can assume that, server didn't perform any process after the runtime_error.

So my query is: 1/ How can I see the runtime_error message in server side console? 2/ Any gRPC default system to pass this incident to the client (Any generic message).

Note: In my real example this runtime_error message related function is coming from another project where I cannot access to modify.


Solution

  • Found the solution.

    In server side I have missed to use the try ... catch in a right way.

    server.cpp

    try
    {
        Check_Value(request);
    }
    catch(const std::exception& e)
    {
        std::cout << e.what() << std::endl; // This will display the error message in server console
        return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, e.what());
    }
    

    An awesome resource regarding error handler in client side is found here.

    client.cpp

    // Class instance, message instances declaration
    
    grpc::Status status = stub->GetAddress(&context, request_, &response);
    if (status.ok())
        // print server response, do the required job
    else
        std::cout << status.error_message() << std::endl;