Skip to content

Commit 71b690a

Browse files
committed
Fix bug in exception interceptor
1 parent e8d109f commit 71b690a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ public GrpcExceptionHandlerInterceptor(GrpcExceptionHandler exceptionHandler) {
6464
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
6565
ServerCallHandler<ReqT, RespT> next) {
6666
Listener<ReqT> listener;
67+
FallbackHandler handler = new FallbackHandler(this.exceptionHandler);
6768
try {
6869
listener = next.startCall(call, headers);
6970
}
7071
catch (Throwable t) {
71-
call.close(this.exceptionHandler.handleException(t).getStatus(), headers(t));
72+
call.close(handler.handleException(t).getStatus(), headers(t));
7273
listener = new Listener<ReqT>() {
7374
};
7475
return listener;
7576
}
76-
return new ExceptionHandlerListener<>(listener, call, new FallbackHandler(this.exceptionHandler));
77+
return new ExceptionHandlerListener<>(listener, call, handler);
7778
}
7879

7980
private static Metadata headers(Throwable t) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.server.exception;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.grpc.server.exception.GrpcExceptionHandlerInterceptor.FallbackHandler;
22+
23+
public class GrpcExceptionHandlerInterceptorTests {
24+
25+
@Test
26+
void testNullStatusHandled() {
27+
assertThat(new FallbackHandler(exception -> null).handleException(new RuntimeException("Test exception")))
28+
.isNotNull();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)