Class GrpcExceptionHandlerInterceptor

java.lang.Object
org.springframework.grpc.server.exception.GrpcExceptionHandlerInterceptor
All Implemented Interfaces:
io.grpc.ServerInterceptor, org.springframework.core.Ordered

@Order(1) public class GrpcExceptionHandlerInterceptor extends Object implements io.grpc.ServerInterceptor, org.springframework.core.Ordered
A gRPC ServerInterceptor that handles exceptions thrown during the processing of gRPC calls. It intercepts the call and wraps the ServerCall.Listener with an GrpcExceptionHandlerInterceptor.ExceptionHandlerListener that catches exceptions in onMessage and onHalfClose methods, and delegates the exception handling to the provided GrpcExceptionHandler.

A fallback mechanism is used to return UNKNOWN in case the GrpcExceptionHandler returns a null.

Ordered just inside Micrometer's ObservationGrpcServerInterceptor (typically @Order(0)). Closing the exception-handled call then goes through ObservationGrpcServerCall#close, so mapped status codes appear on grpc.server metrics instead of falling back to UNKNOWN.

Interceptors that throw from interceptCall (notably authentication) must use an order > ORDER so those exceptions stay inside this handler. Spring Security's gRPC orders are defined relative to this constant.

Author:
Dave Syer, Andrey Litvitski, Yinghuai Fu
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Order used so this interceptor sits just inside Micrometer's gRPC server observation interceptor (@Order(0)) when both are present.

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    <ReqT,RespT>
    io.grpc.ServerCall.Listener<ReqT>
    interceptCall(io.grpc.ServerCall<ReqT,RespT> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<ReqT,RespT> next)
    Intercepts a gRPC server call to handle exceptions.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ORDER

      public static final int ORDER
      Order used so this interceptor sits just inside Micrometer's gRPC server observation interceptor (@Order(0)) when both are present. Security interceptors derive their orders from this value.
      See Also:
  • Constructor Details

    • GrpcExceptionHandlerInterceptor

      public GrpcExceptionHandlerInterceptor(GrpcExceptionHandler exceptionHandler)
  • Method Details

    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
    • interceptCall

      public <ReqT,RespT> io.grpc.ServerCall.Listener<ReqT> interceptCall(io.grpc.ServerCall<ReqT,RespT> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<ReqT,RespT> next)
      Intercepts a gRPC server call to handle exceptions.
      Specified by:
      interceptCall in interface io.grpc.ServerInterceptor
      Type Parameters:
      ReqT - the type of the request message
      RespT - the type of the response message
      Parameters:
      call - the server call object
      headers - the metadata headers for the call
      next - the next server call handler in the interceptor chain
      Returns:
      a listener for the request messages