Skip to content

Commit 026671e

Browse files
author
Dave Syer
committed
Catch exceptions in interceptors as well
1 parent 1a734c1 commit 026671e

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,46 @@ void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
9494
@Nested
9595
@SpringBootTest
9696
@AutoConfigureInProcessTransport
97-
class ServerWithExceptionInInterceptor {
97+
class ServerWithExceptionInInterceptorCall {
98+
99+
@Test
100+
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
101+
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
102+
assertThat(assertThrows(StatusRuntimeException.class,
103+
() -> client.sayHello(HelloRequest.newBuilder().setName("foo").build()))
104+
.getStatus()
105+
.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
106+
}
107+
108+
@TestConfiguration
109+
static class TestConfig {
110+
111+
@Bean
112+
@GlobalServerInterceptor
113+
public ServerInterceptor exceptionInterceptor() {
114+
return new CustomInterceptor();
115+
}
116+
117+
static class CustomInterceptor implements ServerInterceptor {
118+
119+
@Override
120+
public <ReqT, RespT> io.grpc.ServerCall.Listener<ReqT> interceptCall(
121+
io.grpc.ServerCall<ReqT, RespT> call, io.grpc.Metadata headers,
122+
io.grpc.ServerCallHandler<ReqT, RespT> next) {
123+
throw new IllegalArgumentException("test");
124+
125+
}
126+
127+
}
128+
129+
}
130+
131+
}
132+
133+
@Nested
134+
@SpringBootTest
135+
@AutoConfigureInProcessTransport
136+
class ServerWithExceptionInInterceptorListener {
98137

99138
@Test
100139
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {

spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ public GrpcExceptionHandlerInterceptor(GrpcExceptionHandler exceptionHandler) {
6262
@Override
6363
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
6464
ServerCallHandler<ReqT, RespT> next) {
65-
return new ExceptionHandlerListener<>(next.startCall(call, headers), call,
66-
new FallbackHandler(this.exceptionHandler));
65+
Listener<ReqT> listener;
66+
try {
67+
listener = next.startCall(call, headers);
68+
}
69+
catch (Throwable t) {
70+
call.close(this.exceptionHandler.handleException(t), headers(t));
71+
listener = new Listener<ReqT>() {
72+
};
73+
}
74+
return new ExceptionHandlerListener<>(listener, call, new FallbackHandler(this.exceptionHandler));
6775
}
6876

6977
static class ExceptionHandlerListener<ReqT, RespT> extends SimpleForwardingServerCallListener<ReqT> {
@@ -109,11 +117,11 @@ public void onHalfClose() {
109117
}
110118
}
111119

112-
private Metadata headers(Throwable t) {
113-
Metadata result = Status.trailersFromThrowable(t);
114-
return result != null ? result : new Metadata();
115-
}
120+
}
116121

122+
private static Metadata headers(Throwable t) {
123+
Metadata result = Status.trailersFromThrowable(t);
124+
return result != null ? result : new Metadata();
117125
}
118126

119127
static class FallbackHandler implements GrpcExceptionHandler {

0 commit comments

Comments
 (0)