Server side exceptions & errors handling


In server side how can I implemante exceptions/errors handling in order to return response with associated return code (as described here) and a custom error message?

In java-grpc we can have

responseBuilder.setResponseCode(400).setMessage("Service Failed and so and so ...");

But in akka-grpc I found that I can throws an exception
throw new GrpcServiceException(Status.FAILED_PRECONDITION)

I’m not sure it’s the right way.

Please, any idea or example how can I do it ?


For Akka gRPC you are either working with a CompletableFuture/Future or an Akka Stream, both of which can complete with failure. E.g. an exception thrown within a Future or Future.failed. This should be preferred to throwing an exception from the Service implementation however I believe that will work as Akka gRPC internally will call your service from a Future or Stream.