Skip to content

Commit ced5936

Browse files
committed
Fix status trailers in exception handler
1 parent 2dd4b9e commit ced5936

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.grpc.server.exception.GrpcExceptionHandler;
77

8+
import io.grpc.Metadata;
89
import io.grpc.Status;
10+
import io.grpc.StatusException;
911

1012
@SpringBootApplication
1113
public class GrpcServerApplication {
@@ -18,7 +20,11 @@ public static void main(String[] args) {
1820
public GrpcExceptionHandler globalInterceptor() {
1921
return exception -> {
2022
if (exception instanceof IllegalArgumentException) {
21-
return Status.INVALID_ARGUMENT.withDescription(exception.getMessage()).asException();
23+
Metadata metadata = new Metadata();
24+
metadata.put(Metadata.Key.of("error-code", Metadata.ASCII_STRING_MARSHALLER), "INVALID_ARGUMENT");
25+
StatusException result = Status.INVALID_ARGUMENT.withDescription(exception.getMessage())
26+
.asException(metadata);
27+
return result;
2228
}
2329
return null;
2430
};

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import io.grpc.ForwardingServerCallListener;
4646
import io.grpc.ManagedChannel;
47+
import io.grpc.Metadata;
4748
import io.grpc.ServerCall.Listener;
4849
import io.grpc.ServerInterceptor;
4950
import io.grpc.Status.Code;
@@ -77,18 +78,19 @@ class ServerWithException {
7778
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
7879
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
7980
assertThat(assertThrows(StatusRuntimeException.class,
80-
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
81+
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
8182
.getStatus()
82-
.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
83+
.getCode()).isEqualTo(Code.UNKNOWN);
8384
}
8485

8586
@Test
8687
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
8788
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
88-
assertThat(assertThrows(StatusRuntimeException.class,
89-
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
90-
.getStatus()
91-
.getCode()).isEqualTo(Code.UNKNOWN);
89+
StatusRuntimeException status = assertThrows(StatusRuntimeException.class,
90+
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()));
91+
assertThat(status.getStatus().getCode()).isEqualTo(Code.INVALID_ARGUMENT);
92+
assertThat(status.getTrailers().get(Metadata.Key.of("error-code", Metadata.ASCII_STRING_MARSHALLER)))
93+
.isNotNull();
9294
}
9395

9496
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void close(Status status, Metadata trailers) {
3636
if (status.getCode() == Status.Code.UNKNOWN && status.getCause() != null) {
3737
final Throwable cause = status.getCause();
3838
final StatusException statusException = this.exceptionHandler.handleException(cause);
39+
trailers.merge(statusException.getTrailers());
3940
super.close(statusException.getStatus(), trailers);
4041
}
4142
else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void handle(Throwable t) {
147147
catch (Throwable e) {
148148
}
149149
try {
150-
this.call.close(status.getStatus(), headers(t));
150+
this.call.close(status.getStatus(), headers(status));
151151
}
152152
catch (Throwable e) {
153153
throw new IllegalStateException("Failed to close the call", e);

0 commit comments

Comments
 (0)