|
1 | 1 | package org.lognet.springboot.grpc.autoconfigure;
|
2 | 2 |
|
| 3 | +import io.grpc.Status; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.lognet.springboot.grpc.FailureHandlingSupport; |
| 6 | +import org.lognet.springboot.grpc.GRpcErrorHandler; |
3 | 7 | import org.lognet.springboot.grpc.GRpcGlobalInterceptor;
|
| 8 | +import org.lognet.springboot.grpc.recovery.ErrorHandlerAdapter; |
| 9 | +import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler; |
| 10 | +import org.lognet.springboot.grpc.recovery.GRpcExceptionScope; |
| 11 | +import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice; |
4 | 12 | import org.lognet.springboot.grpc.validation.ValidatingInterceptor;
|
5 | 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
6 | 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
9 | 17 | import org.springframework.context.annotation.Configuration;
|
10 | 18 | import org.springframework.context.annotation.Lazy;
|
11 | 19 |
|
| 20 | +import javax.validation.ConstraintViolationException; |
12 | 21 | import javax.validation.Validator;
|
| 22 | +import java.util.Optional; |
13 | 23 |
|
14 | 24 | @Configuration
|
15 |
| -@ConditionalOnClass(Validator.class) |
| 25 | +@ConditionalOnClass({Validator.class}) |
| 26 | + |
16 | 27 | @EnableConfigurationProperties(GRpcValidationProperties.class)
|
17 | 28 | public class GRpcValidationConfiguration {
|
| 29 | + |
| 30 | + |
| 31 | + |
18 | 32 | @Bean
|
19 | 33 | @ConditionalOnBean(Validator.class)
|
20 | 34 | @GRpcGlobalInterceptor
|
21 |
| - public ValidatingInterceptor validatingInterceptor(@Lazy Validator validator,GRpcValidationProperties validationProperties){ |
22 |
| - return new ValidatingInterceptor(validator) |
| 35 | + public ValidatingInterceptor validatingInterceptor(@Lazy Validator validator, GRpcValidationProperties validationProperties,@Lazy FailureHandlingSupport failureHandlingSupport){ |
| 36 | + return new ValidatingInterceptor(validator,failureHandlingSupport) |
23 | 37 | .order(validationProperties.getInterceptorOrder());
|
24 | 38 | }
|
| 39 | + |
| 40 | + @ConditionalOnMissingErrorHandler(ConstraintViolationException.class) |
| 41 | + @Configuration |
| 42 | + static class DefaultValidationHandlerConfig{ |
| 43 | + @GRpcServiceAdvice |
| 44 | + @Slf4j |
| 45 | + public static class DefaultValidationErrorHandler extends ErrorHandlerAdapter { |
| 46 | + |
| 47 | + public DefaultValidationErrorHandler(Optional<GRpcErrorHandler> errorHandler) { |
| 48 | + super(errorHandler); |
| 49 | + } |
| 50 | + |
| 51 | + @GRpcExceptionHandler |
| 52 | + public Status handle(ConstraintViolationException e, GRpcExceptionScope scope){ |
| 53 | + return handle(e,scope.getHintAs(Status.class).get(),scope); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
25 | 61 | }
|
0 commit comments