Skip to content

Commit

Permalink
closes #371
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmlet committed Aug 14, 2023
1 parent 51b4269 commit b6f9a75
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
14 changes: 14 additions & 0 deletions grpc-spring-boot-starter-demo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import org.lognet.springboot.grpc.gradle.ReactiveFeature

buildscript {
ext.kotlin_version = '1.9.0'
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "com.netflix.nebula:nebula-project-plugin:9.4.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id "io.github.lognet.grpc-spring-boot"
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'com.google.protobuf'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
Expand Down Expand Up @@ -115,6 +118,7 @@ dependencies {
reactiveTestImplementation 'org.postgresql:r2dbc-postgresql'
reactiveTestImplementation 'org.postgresql:postgresql'
reactiveTestImplementation "com.playtika.testcontainers:embedded-postgresql:2.2.14"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"


}
Expand All @@ -133,6 +137,16 @@ bootJar{
jar{
enabled true
}
compileKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17
}
}



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.lognet.springboot.grpc.recovery

import io.grpc.Status

fun throwException(status: Status) {
throw status.asException()
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ static class Cfg {
@GRpcService
static class CustomService extends CustomServiceGrpc.CustomServiceImplBase {

@Override
public void anotherCustom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
}

@Override
public void custom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
throw new StatusRuntimeException(Status.FAILED_PRECONDITION);

}

@Override
Expand Down Expand Up @@ -89,7 +95,7 @@ public void onCompleted() {
final Throwable actual = errorFuture.get(20, TimeUnit.SECONDS);
assertThat(actual, notNullValue());
assertThat(actual, isA(StatusRuntimeException.class));
assertThat(((StatusRuntimeException)actual).getStatus(), is(Status.FAILED_PRECONDITION));
assertThat(((StatusRuntimeException) actual).getStatus(), is(Status.FAILED_PRECONDITION));
}

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

@Test
public void statusExceptionTest() {
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(Custom.CustomRequest.newBuilder().build())
);
assertThat(statusRuntimeException.getStatus(), is(Status.FAILED_PRECONDITION));
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.lognet.springboot.grpc;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.*;
import lombok.extern.slf4j.Slf4j;
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver;
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
Expand All @@ -24,11 +21,11 @@ public FailureHandlingSupport(GRpcExceptionHandlerMethodResolver methodResolver)
this.methodResolver = methodResolver;
}

public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata headers) throws RuntimeException {
public void closeCall(Exception e, ServerCall<?, ?> call, Metadata headers) throws RuntimeException {
closeCall(e, call, headers, null);
}

public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata headers, Consumer<GRpcExceptionScope.GRpcExceptionScopeBuilder> customizer) throws RuntimeException {
public void closeCall(Exception e, ServerCall<?, ?> call, Metadata headers, Consumer<GRpcExceptionScope.GRpcExceptionScopeBuilder> customizer) throws RuntimeException {
Optional.ofNullable(EXCEPTION_HANDLED.get()).ifPresent(h -> h.set(true));
if (e == null) {
log.warn("Closing null exception with {}", Status.INTERNAL);
Expand All @@ -38,18 +35,23 @@ public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata header
final Optional<HandlerMethod> handlerMethod = methodResolver.resolveMethodByThrowable(call.getMethodDescriptor().getServiceName(), unwrapped);
if (handlerMethod.isPresent()) {
handle(handlerMethod.get(), call, customizer, e, headers, unwrapped);
} else if (unwrapped instanceof StatusRuntimeException) {
StatusRuntimeException sre = (StatusRuntimeException) unwrapped;
log.warn("Closing call with {}", sre.getStatus());
call.close(sre.getStatus(), Optional.ofNullable(sre.getTrailers()).orElseGet(Metadata::new));
} else if (unwrapped instanceof StatusRuntimeException || unwrapped instanceof StatusException) {
Status status = unwrapped instanceof StatusRuntimeException ?
((StatusRuntimeException) unwrapped).getStatus() :
((StatusException) unwrapped).getStatus();
Metadata metadata = unwrapped instanceof StatusRuntimeException ?
((StatusRuntimeException) unwrapped).getTrailers() :
((StatusException) unwrapped).getTrailers();
log.warn("Closing call with {}", status);
call.close(status, Optional.ofNullable(metadata).orElseGet(Metadata::new));
} else {
log.warn("Closing call with {}", Status.INTERNAL);
call.close(Status.INTERNAL, new Metadata());
}
}
}

private void handle(HandlerMethod handler, ServerCall<?, ?> call, Consumer<GRpcExceptionScope.GRpcExceptionScopeBuilder> customizer, RuntimeException e, Metadata headers, Throwable unwrapped) {
private void handle(HandlerMethod handler, ServerCall<?, ?> call, Consumer<GRpcExceptionScope.GRpcExceptionScopeBuilder> customizer, Exception e, Metadata headers, Throwable unwrapped) {
final GRpcExceptionScope.GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope.builder()
.callHeaders(headers)
.methodCallAttributes(call.getAttributes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void close(Status status, Metadata trailers) {
public void sendMessage(RespT message) {
try {
super.sendMessage(message);
} catch (RuntimeException e) {
} catch (Exception e) {
failureHandlingSupport.closeCall(e, this, headers, b -> b.response(message));
}
}
Expand All @@ -80,7 +80,7 @@ public ServerCall.Listener<ReqT> startCall(ServerCall<ReqT, RespT> call, Metadat
try {

listener = next.startCall(call, headers);
} catch (RuntimeException e) {
} catch (Exception e) {
failureHandlingSupport.closeCall(e, call, headers);
return new ServerCall.Listener<ReqT>() {

Expand All @@ -94,7 +94,7 @@ public void onMessage(ReqT message) {
try {
request = message;
super.onMessage(message);
} catch (RuntimeException e) {
} catch (Exception e) {
blockMessage();
failureHandlingSupport.closeCall(e, call, headers, b -> b.request(request));
}
Expand All @@ -106,7 +106,7 @@ public void onHalfClose() {
if (!callIsClosed.get()) {
super.onHalfClose();
}
} catch (RuntimeException e) {
} catch (Exception e) {
failureHandlingSupport.closeCall(e, call, headers, b -> b.request(request));
}
}
Expand Down

0 comments on commit b6f9a75

Please sign in to comment.