@Documented @Target(value=METHOD) @Retention(value=RUNTIME) public @interface GrpcExceptionHandler
@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);
}
public abstract Class<? extends Throwable>[] value
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.