From 71355e6ff60a7550fe144bd7f079d1941bd82a3b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 13 Jan 2025 10:35:28 +0000 Subject: [PATCH] Allow StatusException to set its own headers --- .../exception/GrpcExceptionHandlerInterceptor.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java index 30ada9c..e73c7d4 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java @@ -85,7 +85,7 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { super.onReady(); } catch (Throwable t) { - this.call.close(this.exceptionHandler.handleException(t), new Metadata()); + this.call.close(this.exceptionHandler.handleException(t), headers(t)); } } @@ -95,7 +95,7 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { super.onMessage(message); } catch (Throwable t) { - this.call.close(this.exceptionHandler.handleException(t), new Metadata()); + this.call.close(this.exceptionHandler.handleException(t), headers(t)); } } @@ -105,10 +105,15 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { super.onHalfClose(); } catch (Throwable t) { - this.call.close(this.exceptionHandler.handleException(t), new Metadata()); + this.call.close(this.exceptionHandler.handleException(t), headers(t)); } } + private Metadata headers(Throwable t) { + Metadata result = Status.trailersFromThrowable(t); + return result != null ? result : new Metadata(); + } + } static class FallbackHandler implements GrpcExceptionHandler {