潘志宝
2024-12-31 c903e9db81839c62dc3a046cfe3434d1a58726ce
iailab-framework/iailab-common-web/src/main/java/com/iailab/framework/web/core/handler/GlobalExceptionHandler.java
@@ -25,6 +25,7 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.dao.DuplicateKeyException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
@@ -94,6 +95,9 @@
        }
        if (ex instanceof AccessDeniedException) {
            return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
        }
        if (ex instanceof DuplicateKeyException) {
            return duplicateKeyExceptionHandler((DuplicateKeyException) ex);
        }
        return defaultExceptionHandler(request, ex);
    }
@@ -199,6 +203,17 @@
    }
    /**
     * 处理业务异常 SQLIntegrityConstraintViolationException
     *
     * 数据库存在重复数据
     */
    @ExceptionHandler(value = DuplicateKeyException.class)
    public CommonResult<?> duplicateKeyExceptionHandler(DuplicateKeyException ex) {
        log.warn("[duplicateKeyExceptionHandler]", ex);
        return CommonResult.error(DATA_REPETITION.getCode(), DATA_REPETITION.getMsg());
    }
    /**
     * 处理业务异常 ServiceException
     *
     * 例如说,商品库存不足,用户手机号已存在。