33import io .grpc .Metadata ;
44import io .grpc .ServerCall ;
55import io .grpc .Status ;
6+ import io .grpc .StatusRuntimeException ;
67import lombok .extern .slf4j .Slf4j ;
78import org .lognet .springboot .grpc .recovery .GRpcExceptionHandlerMethodResolver ;
89import org .lognet .springboot .grpc .recovery .GRpcExceptionScope ;
@@ -16,61 +17,64 @@ public class FailureHandlingSupport {
1617
1718 private final GRpcExceptionHandlerMethodResolver methodResolver ;
1819
19-
20-
2120 public FailureHandlingSupport (GRpcExceptionHandlerMethodResolver methodResolver ) {
2221 this .methodResolver = methodResolver ;
2322 }
2423
25-
26-
27-
2824 public void closeCall (RuntimeException e , ServerCall <?, ?> call , Metadata headers ) throws RuntimeException {
2925 closeCall (e ,call ,headers ,null );
3026 }
3127
3228 public void closeCall ( RuntimeException e , ServerCall <?, ?> call , Metadata headers , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer ) throws RuntimeException {
3329
34-
35-
36- Status statusToSend = Status .INTERNAL ;
37- Metadata metadataToSend = null ;
38-
39- final Optional <HandlerMethod > handlerMethod = methodResolver .resolveMethodByThrowable (call .getMethodDescriptor ().getServiceName (), e );
30+ if ( e == null ) {
31+ log . warn ( "Closing null exception with {}" , Status . INTERNAL );
32+ call . close ( Status .INTERNAL , new Metadata ()) ;
33+ } else {
34+ Throwable unwrapped = GRpcRuntimeExceptionWrapper . unwrap ( e );
35+ final Optional <HandlerMethod > handlerMethod = methodResolver .resolveMethodByThrowable (call .getMethodDescriptor ().getServiceName (), unwrapped );
4036 if (handlerMethod .isPresent ()) {
41- final GRpcExceptionScope .GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope .builder ()
42- .callHeaders (headers )
43- .methodCallAttributes (call .getAttributes ())
44- .methodDescriptor (call .getMethodDescriptor ())
45- .hint (GRpcRuntimeExceptionWrapper .getHint (e ));
46- Optional .ofNullable (customizer )
47- .ifPresent (c -> c .accept (exceptionScopeBuilder ));
48-
49- final GRpcExceptionScope excScope = exceptionScopeBuilder .build ();
37+ handle (handlerMethod .get (), call , customizer , e , headers , unwrapped );
38+ } else if (unwrapped instanceof StatusRuntimeException ) {
39+ StatusRuntimeException sre = (StatusRuntimeException ) unwrapped ;
40+ log .warn ("Closing call with {}" , sre .getStatus ());
41+ call .close (sre .getStatus (), Optional .ofNullable (sre .getTrailers ()).orElseGet (Metadata ::new ));
42+ } else {
43+ log .warn ("Closing call with {}" , Status .INTERNAL );
44+ call .close (Status .INTERNAL , new Metadata ());
45+ }
46+ }
47+ }
5048
51- final HandlerMethod handler = handlerMethod .get ();
49+ private void handle (HandlerMethod handler , ServerCall <?, ?> call , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer , RuntimeException e , Metadata headers , Throwable unwrapped ) {
50+ final GRpcExceptionScope .GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope .builder ()
51+ .callHeaders (headers )
52+ .methodCallAttributes (call .getAttributes ())
53+ .methodDescriptor (call .getMethodDescriptor ())
54+ .hint (GRpcRuntimeExceptionWrapper .getHint (e ));
5255
53- try {
54- statusToSend = handler .invoke (GRpcRuntimeExceptionWrapper .unwrap (e ), excScope );
55- metadataToSend = excScope .getResponseHeaders ();
56- } catch (Exception handlerException ) {
56+ if (customizer != null ) {
57+ customizer .accept (exceptionScopeBuilder );
58+ }
5759
58- org .slf4j .LoggerFactory .getLogger (this .getClass ())
59- .error ("Caught exception while executing handler method {}, returning {} status." ,
60- handler .getMethod (),
61- statusToSend ,
62- handlerException );
60+ final GRpcExceptionScope excScope = exceptionScopeBuilder .build ();
6361
64- }
65- }
62+ try {
63+ Status statusToSend = handler .invoke (unwrapped , excScope );
64+ Metadata metadataToSend = excScope .getResponseHeaders ();
6665
67- log .warn ("Closing call with {}" ,statusToSend , GRpcRuntimeExceptionWrapper . unwrap ( e ) );
66+ log .warn ("Handled exception {} call as {}" , unwrapped . getClass (). getSimpleName (), statusToSend );
6867 call .close (statusToSend , Optional .ofNullable (metadataToSend ).orElseGet (Metadata ::new ));
69-
70-
68+ } catch (Exception handlerException ) {
69+ org .slf4j .LoggerFactory .getLogger (this .getClass ())
70+ .error ("Caught exception while handling exception {} using method {}, closing with {}." ,
71+ unwrapped .getClass ().getSimpleName (),
72+ handler .getMethod (),
73+ Status .INTERNAL ,
74+ handlerException );
75+ call .close (Status .INTERNAL , new Metadata ());
76+ }
7177 }
7278
7379
74-
75-
7680}
0 commit comments