Armeria: Is it possible to use the ExceptionHandler annotation with gRPC services?

Created on 4 Sep 2019  路  3Comments  路  Source: line/armeria

I couldn't find examples with gRPC services, or mention of it not being possible.

question

All 3 comments

It is currently not possible doing that because @ExceptionHandler annotation is for annotated services, which is for building RESTful services with Armeria. However, you can use an interceptor in the official gRPC API or our decorator API.

However, I agree that having such a decorator might be useful for translating exceptions into a response. For example, we wrote a decorator for our Thrift service in our application: https://github.com/line/centraldogma/blob/master/server/src/main/java/com/linecorp/centraldogma/server/internal/thrift/CentralDogmaExceptionTranslator.java

Let me create a feature request issue for you.

An exception raised while serving a gRPC service could be handled by GrpcServiceBuilder.addExceptionMapping() and GrpcServiceBuilder.exceptionMapping(). The new features are released with Armeria 1.3.0.
https://armeria.dev/release-notes/1.3.0

GrpcService.builder()
           .addExceptionMapping(AccessDeniedException.class, Status.UNAUTHENTICATED);
// Or, use GrpcStatusFunction.
GrpcService.builder()
           .exceptionMapping(cause -> {
               if (cause instanceof AccessDeniedException) {
                   return Status.UNAUTHENTICATED;
               }
               if (cause instanceof FileNotFoundException) {
                   return Status.NOT_FOUND;
               }
               return null; // Return null to use Armeria's default exception mapping.
           });
Was this page helpful?
0 / 5 - 0 ratings