Annotation Interface GrpcExceptionHandler


@Documented @Target(METHOD) @Retention(RUNTIME) public @interface GrpcExceptionHandler
Methods annotated with @GrpcExceptionHandler are being mapped to a corresponding Exception, by declaring either in @GrpcExceptionHandler(value = ...) as value or as annotated methods parameter (both is working too).

Return type of annotated methods has to be of type Status, StatusException, StatusRuntimeException or Throwable.

An example without Metadata:

 @GrpcExceptionHandler
 public Status handleIllegalArgumentException(IllegalArgumentException e) {
     return Status.INVALID_ARGUMENT
             .withDescription(e.getMessage())
             .withCause(e);
 }
 
 
With Metadata:
 @GrpcExceptionHandler
    public StatusRuntimeException handleIllegalArgumentException(IllegalArgumentException e){
      Status status = Status.INVALID_ARGUMENT
                            .withDescription(e.getMessage())
                            .withCause(e);
      Metadata myMetadata = new Metadata();
      myMetadata = ...
      return status.asRuntimeException(myMetadata);
    }
  
 
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<? extends Throwable>[]
    Exceptions handled by the annotated method.
  • Element Details

    • value

      Class<? extends Throwable>[] value
      Exceptions handled by the annotated method.

      If empty, will default to any exceptions listed in the method argument list.

      Note: When exception types are set within value, they are prioritized in mapping the exceptions over listed method arguments. And in case method arguments are provided, they must match the types declared with this value.

      Default:
      {}