Parcourir la source

优化错误转换

zhou-hao il y a 4 ans
Parent
commit
c11b7df3e9

+ 11 - 9
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java

@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.web.authorization.exception.AccessDenyException;
 import org.hswebframework.web.authorization.exception.AuthenticationException;
 import org.hswebframework.web.authorization.exception.UnAuthorizedException;
+import org.hswebframework.web.authorization.token.TokenState;
 import org.hswebframework.web.exception.BusinessException;
 import org.hswebframework.web.exception.NotFoundException;
 import org.hswebframework.web.exception.ValidationException;
@@ -27,6 +28,7 @@ import org.springframework.web.server.ServerWebInputException;
 import reactor.core.publisher.Mono;
 
 import javax.validation.ConstraintViolationException;
+import java.util.List;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 
@@ -51,8 +53,8 @@ public class CommonErrorControllerAdvice {
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.UNAUTHORIZED)
-    public Mono<ResponseMessage<?>> handleException(UnAuthorizedException e) {
-        return Mono.just(ResponseMessage.error(401, "unauthorized", e.getMessage()).result(e.getState()));
+    public Mono<ResponseMessage<TokenState>> handleException(UnAuthorizedException e) {
+        return Mono.just(ResponseMessage.<TokenState>error(401, "unauthorized", e.getMessage()).result(e.getState()));
     }
 
     @ExceptionHandler
@@ -69,19 +71,19 @@ public class CommonErrorControllerAdvice {
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(ValidationException e) {
-        return Mono.just(ResponseMessage.error(400, "illegal_argument", e.getMessage()).result(e.getDetails()));
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(ValidationException e) {
+        return Mono.just(ResponseMessage.<List<ValidationException.Detail>>error(400, "illegal_argument", e.getMessage()).result(e.getDetails()));
     }
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(ConstraintViolationException e) {
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(ConstraintViolationException e) {
         return handleException(new ValidationException(e.getMessage(), e.getConstraintViolations()));
     }
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(BindException e) {
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(BindException e) {
         return handleException(new ValidationException(e.getMessage(), e.getBindingResult().getAllErrors()
                 .stream()
                 .filter(FieldError.class::isInstance)
@@ -92,7 +94,7 @@ public class CommonErrorControllerAdvice {
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(WebExchangeBindException e) {
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(WebExchangeBindException e) {
         return handleException(new ValidationException(e.getMessage(), e.getBindingResult().getAllErrors()
                 .stream()
                 .filter(FieldError.class::isInstance)
@@ -104,7 +106,7 @@ public class CommonErrorControllerAdvice {
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(MethodArgumentNotValidException e) {
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(MethodArgumentNotValidException e) {
         return handleException(new ValidationException(e.getMessage(), e.getBindingResult().getAllErrors()
                 .stream()
                 .filter(FieldError.class::isInstance)
@@ -186,7 +188,7 @@ public class CommonErrorControllerAdvice {
 
     @ExceptionHandler
     @ResponseStatus(HttpStatus.BAD_REQUEST)
-    public Mono<ResponseMessage<?>> handleException(ServerWebInputException e) {
+    public Mono<ResponseMessage<List<ValidationException.Detail>>> handleException(ServerWebInputException e) {
         Throwable exception = e;
         do {
             exception = exception.getCause();

+ 5 - 0
hsweb-core/pom.xml

@@ -73,6 +73,11 @@
             <artifactId>reactor-core</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>io.swagger.core.v3</groupId>
+            <artifactId>swagger-annotations</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>

+ 5 - 1
hsweb-core/src/main/java/org/hswebframework/web/exception/ValidationException.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.exception;
 
+import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.Setter;
@@ -32,7 +33,7 @@ public class ValidationException extends BusinessException {
         this.details = details;
     }
 
-    public ValidationException(String message, Set<? extends ConstraintViolation> violations) {
+    public ValidationException(String message, Set<? extends ConstraintViolation<?>> violations) {
         super(message);
         if (null != violations && !violations.isEmpty()) {
             details = new ArrayList<>();
@@ -46,10 +47,13 @@ public class ValidationException extends BusinessException {
     @Setter
     @AllArgsConstructor
     public static class Detail {
+        @Schema(description = "字段")
         String property;
 
+        @Schema(description = "说明")
         String message;
 
+        @Schema(description = "详情")
         Object detail;
     }
 }