Browse Source

增加异常溯源支持

zhouhao 2 years ago
parent
commit
3416fd24d6

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

@@ -19,7 +19,7 @@ import java.util.Objects;
  */
 @Getter
 @Setter(AccessLevel.PROTECTED)
-public class I18nSupportException extends RuntimeException {
+public class I18nSupportException extends TraceSourceException {
 
     /**
      * 消息code,在message.properties文件中定义的key

+ 50 - 0
hsweb-core/src/main/java/org/hswebframework/web/exception/TraceSourceException.java

@@ -0,0 +1,50 @@
+package org.hswebframework.web.exception;
+
+import javax.annotation.Nullable;
+import java.util.Optional;
+
+/**
+ * 支持溯源的异常,通过{@link TraceSourceException#withSource(Object) }来标识异常的源头.
+ * 在捕获异常的地方通过获取异常源来处理一些逻辑,比如判断是由哪条数据发生的错误等操作.
+ *
+ * @author zhouhao
+ * @since 4.0.15
+ */
+public class TraceSourceException extends RuntimeException {
+
+    private Object source;
+
+    public TraceSourceException() {
+
+    }
+
+    public TraceSourceException(String message) {
+        super(message);
+    }
+
+    public TraceSourceException(Throwable e) {
+        super(e);
+    }
+
+    public TraceSourceException(String message, Throwable e) {
+        super(message, e);
+    }
+
+    public Optional<Object> sourceOptional() {
+        return Optional.ofNullable(source);
+    }
+
+    @Nullable
+    public Object getSource() {
+        return source;
+    }
+
+    public TraceSourceException withSource(Object source) {
+        this.source = source;
+        return self();
+    }
+
+    protected TraceSourceException self() {
+        return this;
+    }
+}

+ 3 - 3
hsweb-core/src/main/java/org/hswebframework/web/validator/ValidatorUtils.java

@@ -38,7 +38,7 @@ public final class ValidatorUtils {
     public static <T> T tryValidate(T bean, Class<?>... group) {
         Set<ConstraintViolation<T>> violations = getValidator().validate(bean, group);
         if (!violations.isEmpty()) {
-            throw new ValidationException(violations);
+            throw new ValidationException(violations).withSource(bean);
         }
 
         return bean;
@@ -47,7 +47,7 @@ public final class ValidatorUtils {
     public static <T> T tryValidate(T bean, String property, Class<?>... group) {
         Set<ConstraintViolation<T>> violations = getValidator().validateProperty(bean, property, group);
         if (!violations.isEmpty()) {
-            throw new ValidationException(violations);
+            throw new ValidationException(violations).withSource(bean);
         }
 
         return bean;
@@ -56,7 +56,7 @@ public final class ValidatorUtils {
     public static <T> void tryValidate(Class<T> bean, String property, Object value, Class<?>... group) {
         Set<ConstraintViolation<T>> violations = getValidator().validateValue(bean, property, value, group);
         if (!violations.isEmpty()) {
-            throw new ValidationException(violations);
+            throw new ValidationException(violations).withSource(value);
         }
     }