Browse Source

优化异常处理

zhou-hao 3 years ago
parent
commit
69962f8afc

+ 1 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java

@@ -42,7 +42,7 @@ public class AccessDenyException extends I18nSupportException {
     }
 
     public AccessDenyException(String message, String code, Throwable cause) {
-        super(message, cause);
+        super(message, cause,code);
         this.code = code;
     }
 }

+ 1 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisUserTokenManager.java

@@ -244,7 +244,7 @@ public class RedisUserTokenManager implements UserTokenManager {
                         return userIsLoggedIn(userId)
                                 .flatMap(r -> {
                                     if (r) {
-                                        return Mono.error(new AccessDenyException("error.logged_in_elsewhere", TokenState.deny.getValue(), null));
+                                        return Mono.error(new AccessDenyException("error.logged_in_elsewhere", TokenState.deny.getValue()));
                                     }
                                     return doSign;
                                 });

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

@@ -253,7 +253,7 @@ public class CommonErrorControllerAdvice {
     public Mono<ResponseMessage<Object>> handleException(I18nSupportException e) {
         return LocaleUtils
                 .resolveThrowable(e,
-                                  (err, msg) -> ResponseMessage.error(400, err.getCode(), msg));
+                                  (err, msg) -> ResponseMessage.error(400, err.getI18nCode(), msg));
     }
 
 }

+ 4 - 6
hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java

@@ -6,8 +6,6 @@ import lombok.Getter;
 import lombok.Setter;
 import org.hswebframework.web.i18n.LocaleUtils;
 
-import java.util.Locale;
-
 /**
  * 支持国际化消息的异常,code为
  *
@@ -22,7 +20,7 @@ public class I18nSupportException extends RuntimeException {
     /**
      * 消息code,在message.properties文件中定义的key
      */
-    private String code;
+    private String i18nCode;
 
     /**
      * 消息参数
@@ -35,14 +33,14 @@ public class I18nSupportException extends RuntimeException {
 
     public I18nSupportException(String code, Object... args) {
         super(code);
-        this.code = code;
+        this.i18nCode = code;
         this.args = args;
     }
 
     public I18nSupportException(String code, Throwable cause, Object... args) {
         super(code, cause);
         this.args = args;
-        this.code = code;
+        this.i18nCode = code;
     }
 
     @Override
@@ -52,6 +50,6 @@ public class I18nSupportException extends RuntimeException {
 
     @Override
     public String getLocalizedMessage() {
-        return LocaleUtils.resolveMessage(code, args);
+        return LocaleUtils.resolveMessage(i18nCode, args);
     }
 }

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

@@ -40,9 +40,9 @@ public class ValidationException extends I18nSupportException {
         ConstraintViolation<?> first = violations.iterator().next();
         if (Objects.equals(first.getMessageTemplate(), first.getMessage())) {
             //模版和消息相同,说明是自定义的message,而不是已经通过i18n获取的.
-            setCode(first.getMessage());
+            setI18nCode(first.getMessage());
         } else {
-            setCode("validation.property_validate_failed");
+            setI18nCode("validation.property_validate_failed");
         }
         String property = first.getPropertyPath().toString();
 

+ 1 - 1
hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java

@@ -154,7 +154,7 @@ public final class LocaleUtils {
     public static <S extends I18nSupportException, R> Mono<R> resolveThrowable(MessageSource messageSource,
                                                                                S source,
                                                                                BiFunction<S, String, R> mapper) {
-        return doWithReactive(messageSource, source, I18nSupportException::getCode, mapper, source.getArgs());
+        return doWithReactive(messageSource, source, I18nSupportException::getI18nCode, mapper, source.getArgs());
     }
 
     /**