diff --git a/samples/grpc-reactive/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-reactive/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java index f6fd6fb..fae52ff 100644 --- a/samples/grpc-reactive/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java +++ b/samples/grpc-reactive/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java @@ -23,9 +23,9 @@ public class GrpcServerApplication { return ex -> { if (ex instanceof IllegalArgumentException) { log.error("Error in grpc exception", ex); - return Status.INVALID_ARGUMENT.withDescription(ex.getMessage()); + return Status.INVALID_ARGUMENT.withDescription(ex.getMessage()).asException(); } - return Status.INTERNAL.withCause(ex).withDescription(ex.getMessage()); + return Status.INTERNAL.withCause(ex).withDescription(ex.getMessage()).asException(); }; } diff --git a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java index 0caa6ce..3a717b2 100644 --- a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java +++ b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java @@ -18,7 +18,7 @@ public class GrpcServerApplication { public GrpcExceptionHandler globalInterceptor() { return exception -> { if (exception instanceof IllegalArgumentException) { - return Status.INVALID_ARGUMENT.withDescription(exception.getMessage()); + return Status.INVALID_ARGUMENT.withDescription(exception.getMessage()).asException(); } return null; }; diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/CompositeGrpcExceptionHandler.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/CompositeGrpcExceptionHandler.java index 915aa5a..6c87ac2 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/CompositeGrpcExceptionHandler.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/CompositeGrpcExceptionHandler.java @@ -15,7 +15,7 @@ */ package org.springframework.grpc.server.exception; -import io.grpc.Status; +import io.grpc.StatusException; public class CompositeGrpcExceptionHandler implements GrpcExceptionHandler { @@ -26,9 +26,9 @@ public class CompositeGrpcExceptionHandler implements GrpcExceptionHandler { } @Override - public Status handleException(Throwable exception) { + public StatusException handleException(Throwable exception) { for (GrpcExceptionHandler exceptionHandler : this.exceptionHandlers) { - Status status = exceptionHandler.handleException(exception); + StatusException status = exceptionHandler.handleException(exception); if (status != null) { return status; } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandler.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandler.java index 30c3319..905a6d0 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandler.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandler.java @@ -15,7 +15,7 @@ */ package org.springframework.grpc.server.exception; -import io.grpc.Status; +import io.grpc.StatusException; /** * Defines an exception handler for handling exceptions that occur during gRPC server-side @@ -32,6 +32,6 @@ public interface GrpcExceptionHandler { * @return the status to return to the client, or {@code null} if the exception cannot * be classified */ - Status handleException(Throwable exception); + StatusException handleException(Throwable exception); } 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 4827855..61bf1da 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 @@ -25,6 +25,7 @@ import io.grpc.ServerCall.Listener; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; import io.grpc.Status; +import io.grpc.StatusException; /** * A gRPC {@link ServerInterceptor} that handles exceptions thrown during the processing @@ -67,7 +68,7 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { listener = next.startCall(call, headers); } catch (Throwable t) { - call.close(this.exceptionHandler.handleException(t), headers(t)); + call.close(this.exceptionHandler.handleException(t).getStatus(), headers(t)); listener = new Listener() { }; return listener; @@ -136,14 +137,14 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { private void handle(Throwable t) { this.exception = t; - Status status = Status.fromThrowable(t); + StatusException status = Status.fromThrowable(t).asException(); try { status = this.exceptionHandler.handleException(t); } catch (Throwable e) { } try { - this.call.close(status, headers(t)); + this.call.close(status.getStatus(), headers(t)); } catch (Throwable e) { throw new IllegalStateException("Failed to close the call", e); @@ -161,9 +162,9 @@ public class GrpcExceptionHandlerInterceptor implements ServerInterceptor { } @Override - public Status handleException(Throwable exception) { - Status status = this.exceptionHandler.handleException(exception); - return status != null ? status : Status.fromThrowable(exception); + public StatusException handleException(Throwable exception) { + StatusException status = this.exceptionHandler.handleException(exception); + return status != null ? status : Status.fromThrowable(exception).asException(); } } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/ReactiveStubBeanDefinitionRegistrar.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/ReactiveStubBeanDefinitionRegistrar.java index 81cc438..0228157 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/ReactiveStubBeanDefinitionRegistrar.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/ReactiveStubBeanDefinitionRegistrar.java @@ -33,7 +33,6 @@ import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ReflectionUtils; import io.grpc.BindableService; -import io.grpc.Status; import io.grpc.StatusException; /** @@ -80,8 +79,8 @@ public class ReactiveStubBeanDefinitionRegistrar implements ImportBeanDefinition .toArray(GrpcExceptionHandler[]::new); this.handler = new CompositeGrpcExceptionHandler(handlers); } - Status status = this.handler.handleException(throwable); - return status != null ? new StatusException(status) : throwable; + StatusException status = this.handler.handleException(throwable); + return status != null ? status : throwable; } @Override diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/security/SecurityGrpcExceptionHandler.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/security/SecurityGrpcExceptionHandler.java index 24b1f38..867b847 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/security/SecurityGrpcExceptionHandler.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/security/SecurityGrpcExceptionHandler.java @@ -23,24 +23,25 @@ import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.AuthenticationException; import io.grpc.Status; +import io.grpc.StatusException; public class SecurityGrpcExceptionHandler implements GrpcExceptionHandler { private static final Log logger = LogFactory.getLog(SecurityGrpcExceptionHandler.class); @Override - public Status handleException(Throwable exception) { + public StatusException handleException(Throwable exception) { if (exception instanceof AuthenticationException) { if (logger.isDebugEnabled()) { logger.error("Failed to authenticate", exception); } - return Status.UNAUTHENTICATED.withDescription(exception.getMessage()); + return Status.UNAUTHENTICATED.withDescription(exception.getMessage()).asException(); } if (exception instanceof AccessDeniedException) { if (logger.isDebugEnabled()) { logger.error("Failed to authorize", exception); } - return Status.PERMISSION_DENIED.withDescription(exception.getMessage()); + return Status.PERMISSION_DENIED.withDescription(exception.getMessage()).asException(); } return null; }