C++: Custom method handlers

Skip to first unread message

Davor Cubranic

Jun 15, 2022, 10:35:41 PMJun 15
to grpc.io

I'm trying to find a way to add custom method handlers on my service, but cannot see a way with the current API. 

As far as I can tell, generated Service classes call `grpc::Service::AddMethod()` with an `::grpc::internal::RpcMethodHandler` whose handler function is a closure that just contains `return service->RpcMethod(ctx, req, resp);`. In turn, `RpcMethodHandler::RunHandler()` uses the `CatchingFunctionHandler` to run the handler function within a try-catch and return StatusCode::UNKNOWN if it throws.

Instead, I'd like to have a slight variant on the CatchingFunctionHandler that does a bit more with exceptions (log the , etc.) However, it doesn't look like the API allows for customization in this area: `AddMethod()` is not virtual, so I cannot override it in my service subclass, and once a method been added, it is private to grpc::Service, and I cannot access it to modify or customize its handler any more.

Is there a way this can be done? Or if not, any plans to make it possible?


Mark D. Roth

Jun 22, 2022, 1:40:19 PMJun 22
to Davor Cubranic, grpc.io
We don't actively support exceptions, since our style guide prohibits them (https://google.github.io/styleguide/cppguide.html#Exceptions), so we're unlikely to put any work into providing hooks for better exception handling.  However, if you wanted to propose a gRFC for a new API that would do something like this, we might be able to advise you as to as reasonable way to do it.

Note that none of the code you're referring to here is a public API, so even if you found a way to make this work with the existing code, it would not be a supported use, and there would be no guarantee that it wouldn't break as the code evolves.

Confidentiality Notice: This communication contains information which is confidential and may also be legally privileged.  If you are not the intended recipient(s), you are hereby notified that any disclosure, copying, distribution, or use of this communication is strictly prohibited.  If you have received this communication in error, please notify the sender and promptly delete from your system the entirety of the communication and any copies thereof.  

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 grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/766bd8c0-36b2-4898-8f1c-3dffafb18c4fn%40googlegroups.com.

Mark D. Roth <ro...@google.com>
Software Engineer
Google, Inc.
Reply all
Reply to author
0 new messages