-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecate GRpcErrorHandler in favor of GRpcExceptionHandler and GRpcS…
…erviceAdvice
- Loading branch information
Alexander Furer
committed
Oct 14, 2021
1 parent
4efb349
commit 19fc751
Showing
22 changed files
with
520 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
...ot-starter/src/main/java/org/lognet/springboot/grpc/FailureHandlingServerInterceptor.java
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
...-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/FailureHandlingSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.lognet.springboot.grpc; | ||
|
||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.Status; | ||
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver; | ||
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope; | ||
import org.lognet.springboot.grpc.recovery.GRpcRuntimeExceptionWrapper; | ||
import org.lognet.springboot.grpc.recovery.HandlerMethod; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public class FailureHandlingSupport { | ||
|
||
private final GRpcExceptionHandlerMethodResolver methodResolver; | ||
|
||
public FailureHandlingSupport(GRpcExceptionHandlerMethodResolver methodResolver) { | ||
this.methodResolver = methodResolver; | ||
} | ||
|
||
public void closeCall(RuntimeException e, ServerCall<?, ?> call, Metadata headers, Consumer<GRpcExceptionScope.GRpcExceptionScopeBuilder> customizer) throws RuntimeException { | ||
|
||
|
||
final Optional<HandlerMethod> handlerMethod = methodResolver.resolveMethodByThrowable(call.getMethodDescriptor().getServiceName(), e); | ||
if (handlerMethod.isPresent()) { | ||
final GRpcExceptionScope.GRpcExceptionScopeBuilder exceptionScopeBuilder = GRpcExceptionScope.builder() | ||
.callHeaders(headers) | ||
.methodCallAttributes(call.getAttributes()) | ||
.methodDescriptor(call.getMethodDescriptor()) | ||
.hint(GRpcRuntimeExceptionWrapper.getHint(e)); | ||
customizer.accept(exceptionScopeBuilder); | ||
|
||
final GRpcExceptionScope excScope = exceptionScopeBuilder.build(); | ||
|
||
final HandlerMethod handler = handlerMethod.get(); | ||
|
||
|
||
Status statusToSend = Status.INTERNAL; | ||
try { | ||
statusToSend = handler.invoke(GRpcRuntimeExceptionWrapper.unwrap(e),excScope ); | ||
}catch (Exception handlerException){ | ||
|
||
org.slf4j.LoggerFactory.getLogger(this.getClass()) | ||
.error("Caught exception while executing handler method {}, returning {} status.", | ||
handler.getMethod(), | ||
statusToSend, | ||
handlerException); | ||
|
||
} | ||
call.close(statusToSend, excScope.getResponseHeaders()); | ||
|
||
|
||
} else { | ||
call.close(Status.INTERNAL,new Metadata()); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...t-starter/src/main/java/org/lognet/springboot/grpc/MessageBlockingServerCallListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.lognet.springboot.grpc; | ||
|
||
import io.grpc.ForwardingServerCallListener; | ||
import io.grpc.ServerCall; | ||
|
||
|
||
public class MessageBlockingServerCallListener<R> extends ForwardingServerCallListener.SimpleForwardingServerCallListener<R> { | ||
|
||
|
||
private volatile boolean messageBlocked = false; | ||
|
||
|
||
public MessageBlockingServerCallListener(ServerCall.Listener<R> delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void onHalfClose() { | ||
// If the message was blocked, downstream never had a chance to react to it. Hence, the half-close signal would look like | ||
// an error to them. So we do not propagate the signal in that case. | ||
if (!messageBlocked) { | ||
super.onHalfClose(); | ||
} | ||
} | ||
|
||
protected void blockMessage() { | ||
messageBlocked = true; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../main/java/org/lognet/springboot/grpc/autoconfigure/ConditionalOnMissingErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.lognet.springboot.grpc.autoconfigure; | ||
|
||
|
||
import org.springframework.context.annotation.Conditional; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ ElementType.TYPE , ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Conditional(OnMissingErrorHandlerCondition.class) | ||
public @interface ConditionalOnMissingErrorHandler { | ||
Class<? extends Throwable> value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rc/main/java/org/lognet/springboot/grpc/autoconfigure/OnMissingErrorHandlerCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.lognet.springboot.grpc.autoconfigure; | ||
|
||
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler; | ||
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice; | ||
import org.lognet.springboot.grpc.recovery.HandlerMethod; | ||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | ||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.MethodIntrospector; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
public class OnMissingErrorHandlerCondition extends SpringBootCondition { | ||
|
||
|
||
@Override | ||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
Class<? extends Throwable> exc = (Class<? extends Throwable>) metadata | ||
.getAnnotationAttributes(ConditionalOnMissingErrorHandler.class.getName()) | ||
.get("value"); | ||
|
||
ReflectionUtils.MethodFilter f = method -> AnnotatedElementUtils.hasAnnotation(method, GRpcExceptionHandler.class); | ||
for(String adviceBeanName:context.getBeanFactory().getBeanNamesForAnnotation(GRpcServiceAdvice.class)){ | ||
final String beanClassName = context.getBeanFactory().getBeanDefinition(adviceBeanName) | ||
.getBeanClassName(); | ||
|
||
try { | ||
for (Method method : MethodIntrospector.selectMethods(Class.forName(beanClassName), f)) { | ||
final Optional<Class<? extends Throwable>> handledException = HandlerMethod.getHandledException(method, false); | ||
if(handledException.isPresent() && handledException.get().isAssignableFrom(exc)){ | ||
return ConditionOutcome.noMatch(String.format("Found %s handler at %s.%s", | ||
handledException.get().getName(), | ||
beanClassName, | ||
method.getName() | ||
)); | ||
} | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
}; | ||
|
||
return ConditionOutcome.match(); | ||
} | ||
} |
Oops, something went wrong.