1
1
package org .lognet .springboot .grpc ;
2
2
3
- import io .grpc .Metadata ;
4
- import io .grpc .ServerCall ;
5
- import io .grpc .Status ;
6
- import io .grpc .StatusRuntimeException ;
3
+ import io .grpc .*;
7
4
import lombok .extern .slf4j .Slf4j ;
8
5
import org .lognet .springboot .grpc .recovery .GRpcExceptionHandlerMethodResolver ;
9
6
import org .lognet .springboot .grpc .recovery .GRpcExceptionScope ;
@@ -24,11 +21,11 @@ public FailureHandlingSupport(GRpcExceptionHandlerMethodResolver methodResolver)
24
21
this .methodResolver = methodResolver ;
25
22
}
26
23
27
- public void closeCall (RuntimeException e , ServerCall <?, ?> call , Metadata headers ) throws RuntimeException {
24
+ public void closeCall (Exception e , ServerCall <?, ?> call , Metadata headers ) throws RuntimeException {
28
25
closeCall (e , call , headers , null );
29
26
}
30
27
31
- public void closeCall (RuntimeException e , ServerCall <?, ?> call , Metadata headers , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer ) throws RuntimeException {
28
+ public void closeCall (Exception e , ServerCall <?, ?> call , Metadata headers , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer ) throws RuntimeException {
32
29
Optional .ofNullable (EXCEPTION_HANDLED .get ()).ifPresent (h -> h .set (true ));
33
30
if (e == null ) {
34
31
log .warn ("Closing null exception with {}" , Status .INTERNAL );
@@ -38,18 +35,23 @@ public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata header
38
35
final Optional <HandlerMethod > handlerMethod = methodResolver .resolveMethodByThrowable (call .getMethodDescriptor ().getServiceName (), unwrapped );
39
36
if (handlerMethod .isPresent ()) {
40
37
handle (handlerMethod .get (), call , customizer , e , headers , unwrapped );
41
- } else if (unwrapped instanceof StatusRuntimeException ) {
42
- StatusRuntimeException sre = (StatusRuntimeException ) unwrapped ;
43
- log .warn ("Closing call with {}" , sre .getStatus ());
44
- call .close (sre .getStatus (), Optional .ofNullable (sre .getTrailers ()).orElseGet (Metadata ::new ));
38
+ } else if (unwrapped instanceof StatusRuntimeException || unwrapped instanceof StatusException ) {
39
+ Status status = unwrapped instanceof StatusRuntimeException ?
40
+ ((StatusRuntimeException ) unwrapped ).getStatus () :
41
+ ((StatusException ) unwrapped ).getStatus ();
42
+ Metadata metadata = unwrapped instanceof StatusRuntimeException ?
43
+ ((StatusRuntimeException ) unwrapped ).getTrailers () :
44
+ ((StatusException ) unwrapped ).getTrailers ();
45
+ log .warn ("Closing call with {}" , status );
46
+ call .close (status , Optional .ofNullable (metadata ).orElseGet (Metadata ::new ));
45
47
} else {
46
48
log .warn ("Closing call with {}" , Status .INTERNAL );
47
49
call .close (Status .INTERNAL , new Metadata ());
48
50
}
49
51
}
50
52
}
51
53
52
- private void handle (HandlerMethod handler , ServerCall <?, ?> call , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer , RuntimeException e , Metadata headers , Throwable unwrapped ) {
54
+ private void handle (HandlerMethod handler , ServerCall <?, ?> call , Consumer <GRpcExceptionScope .GRpcExceptionScopeBuilder > customizer , Exception e , Metadata headers , Throwable unwrapped ) {
53
55
final GRpcExceptionScope .GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope .builder ()
54
56
.callHeaders (headers )
55
57
.methodCallAttributes (call .getAttributes ())
0 commit comments