Skip to content

Commit b6f9a75

Browse files
committed
closes #371
1 parent 51b4269 commit b6f9a75

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

Diff for: grpc-spring-boot-starter-demo/build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import org.lognet.springboot.grpc.gradle.ReactiveFeature
22

33
buildscript {
4+
ext.kotlin_version = '1.9.0'
45
repositories {
56
mavenCentral()
67
}
78
dependencies {
89
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
910
classpath "com.netflix.nebula:nebula-project-plugin:9.4.0"
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1012
}
1113
}
1214
plugins {
1315
id "io.github.lognet.grpc-spring-boot"
1416
}
1517

1618
apply plugin: 'java'
19+
apply plugin: 'kotlin'
1720
apply plugin: 'com.google.protobuf'
1821
apply plugin: 'org.springframework.boot'
1922
apply plugin: 'io.spring.dependency-management'
@@ -115,6 +118,7 @@ dependencies {
115118
reactiveTestImplementation 'org.postgresql:r2dbc-postgresql'
116119
reactiveTestImplementation 'org.postgresql:postgresql'
117120
reactiveTestImplementation "com.playtika.testcontainers:embedded-postgresql:2.2.14"
121+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
118122

119123

120124
}
@@ -133,6 +137,16 @@ bootJar{
133137
jar{
134138
enabled true
135139
}
140+
compileKotlin {
141+
kotlinOptions {
142+
jvmTarget = JavaVersion.VERSION_17
143+
}
144+
}
145+
compileTestKotlin {
146+
kotlinOptions {
147+
jvmTarget = JavaVersion.VERSION_17
148+
}
149+
}
136150

137151

138152

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.lognet.springboot.grpc.recovery
2+
3+
import io.grpc.Status
4+
5+
fun throwException(status: Status) {
6+
throw status.asException()
7+
}

Diff for: grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ static class Cfg {
4747
@GRpcService
4848
static class CustomService extends CustomServiceGrpc.CustomServiceImplBase {
4949

50+
@Override
51+
public void anotherCustom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
52+
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
53+
}
54+
5055
@Override
5156
public void custom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
5257
throw new StatusRuntimeException(Status.FAILED_PRECONDITION);
58+
5359
}
5460

5561
@Override
@@ -89,7 +95,7 @@ public void onCompleted() {
8995
final Throwable actual = errorFuture.get(20, TimeUnit.SECONDS);
9096
assertThat(actual, notNullValue());
9197
assertThat(actual, isA(StatusRuntimeException.class));
92-
assertThat(((StatusRuntimeException)actual).getStatus(), is(Status.FAILED_PRECONDITION));
98+
assertThat(((StatusRuntimeException) actual).getStatus(), is(Status.FAILED_PRECONDITION));
9399
}
94100

95101
@Test
@@ -100,4 +106,12 @@ public void statusRuntimeExceptionTest() {
100106
assertThat(statusRuntimeException.getStatus(), is(Status.FAILED_PRECONDITION));
101107
}
102108

109+
@Test
110+
public void statusExceptionTest() {
111+
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
112+
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(Custom.CustomRequest.newBuilder().build())
113+
);
114+
assertThat(statusRuntimeException.getStatus(), is(Status.FAILED_PRECONDITION));
115+
}
116+
103117
}

Diff for: grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/FailureHandlingSupport.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.lognet.springboot.grpc;
22

3-
import io.grpc.Metadata;
4-
import io.grpc.ServerCall;
5-
import io.grpc.Status;
6-
import io.grpc.StatusRuntimeException;
3+
import io.grpc.*;
74
import lombok.extern.slf4j.Slf4j;
85
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver;
96
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
@@ -24,11 +21,11 @@ public FailureHandlingSupport(GRpcExceptionHandlerMethodResolver methodResolver)
2421
this.methodResolver = methodResolver;
2522
}
2623

27-
public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata headers) throws RuntimeException {
24+
public void closeCall(Exception e, ServerCall<?, ?> call, Metadata headers) throws RuntimeException {
2825
closeCall(e, call, headers, null);
2926
}
3027

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 {
3229
Optional.ofNullable(EXCEPTION_HANDLED.get()).ifPresent(h -> h.set(true));
3330
if (e == null) {
3431
log.warn("Closing null exception with {}", Status.INTERNAL);
@@ -38,18 +35,23 @@ public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata header
3835
final Optional<HandlerMethod> handlerMethod = methodResolver.resolveMethodByThrowable(call.getMethodDescriptor().getServiceName(), unwrapped);
3936
if (handlerMethod.isPresent()) {
4037
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));
4547
} else {
4648
log.warn("Closing call with {}", Status.INTERNAL);
4749
call.close(Status.INTERNAL, new Metadata());
4850
}
4951
}
5052
}
5153

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) {
5355
final GRpcExceptionScope.GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope.builder()
5456
.callHeaders(headers)
5557
.methodCallAttributes(call.getAttributes())

Diff for: grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/recovery/GRpcExceptionHandlerInterceptor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void close(Status status, Metadata trailers) {
6363
public void sendMessage(RespT message) {
6464
try {
6565
super.sendMessage(message);
66-
} catch (RuntimeException e) {
66+
} catch (Exception e) {
6767
failureHandlingSupport.closeCall(e, this, headers, b -> b.response(message));
6868
}
6969
}
@@ -80,7 +80,7 @@ public ServerCall.Listener<ReqT> startCall(ServerCall<ReqT, RespT> call, Metadat
8080
try {
8181

8282
listener = next.startCall(call, headers);
83-
} catch (RuntimeException e) {
83+
} catch (Exception e) {
8484
failureHandlingSupport.closeCall(e, call, headers);
8585
return new ServerCall.Listener<ReqT>() {
8686

@@ -94,7 +94,7 @@ public void onMessage(ReqT message) {
9494
try {
9595
request = message;
9696
super.onMessage(message);
97-
} catch (RuntimeException e) {
97+
} catch (Exception e) {
9898
blockMessage();
9999
failureHandlingSupport.closeCall(e, call, headers, b -> b.request(request));
100100
}
@@ -106,7 +106,7 @@ public void onHalfClose() {
106106
if (!callIsClosed.get()) {
107107
super.onHalfClose();
108108
}
109-
} catch (RuntimeException e) {
109+
} catch (Exception e) {
110110
failureHandlingSupport.closeCall(e, call, headers, b -> b.request(request));
111111
}
112112
}

0 commit comments

Comments
 (0)