Change method signature for exception handler
This commit is contained in:
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ReqT>() {
|
||||
};
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user