🎨 调整异常信息兜底方式,只在prod环境处理
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package com.hccake.ballcat.common.conf.exception;
|
||||
|
||||
import com.hccake.ballcat.common.core.constant.GlobalConstants;
|
||||
import com.hccake.ballcat.common.core.exception.BusinessException;
|
||||
import com.hccake.ballcat.common.core.exception.handler.GlobalExceptionHandler;
|
||||
import com.hccake.ballcat.common.core.result.R;
|
||||
import com.hccake.ballcat.common.core.result.SystemResultCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.core.SpringSecurityMessageSource;
|
||||
@@ -18,7 +19,6 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.validation.ValidationException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -31,6 +31,8 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class GlobalExceptionHandlerResolver {
|
||||
private final GlobalExceptionHandler globalExceptionHandler;
|
||||
@Value("${spring.profiles.active}")
|
||||
private String profile;
|
||||
|
||||
/**
|
||||
* 全局异常捕获
|
||||
@@ -43,35 +45,9 @@ public class GlobalExceptionHandlerResolver {
|
||||
public R<String> handleGlobalException(Exception e) {
|
||||
log.error("全局异常信息 ex={}", e.getMessage(), e);
|
||||
globalExceptionHandler.handle(e);
|
||||
return R.failed(SystemResultCode.SERVER_ERROR, e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库约束异常处理
|
||||
* 防止数据库信息泄露
|
||||
* @param e DataIntegrityViolationException
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler({DataIntegrityViolationException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public R<String> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
||||
log.error("违反数据库约束异常信息 ex={}", e.getMessage(), e);
|
||||
globalExceptionHandler.handle(e);
|
||||
return R.failed(SystemResultCode.SERVER_ERROR, "违反数据库约束");
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL 异常处理
|
||||
* 防止数据库信息泄露
|
||||
* @param e SQLException
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler({SQLException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public R<String> handleSqlException(SQLException e) {
|
||||
log.error("SQL异常信息 ex={}", e.getMessage(), e);
|
||||
globalExceptionHandler.handle(e);
|
||||
return R.failed(SystemResultCode.SERVER_ERROR, "数据库处理异常");
|
||||
// 当为生产环境, 不适合把具体的异常信息展示给用户, 比如数据库异常信息.
|
||||
String errorMsg = GlobalConstants.ENV_PROD.equals(profile) ? "系统异常,请联系管理员" : e.getLocalizedMessage();
|
||||
return R.failed(SystemResultCode.SERVER_ERROR, errorMsg);
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +68,6 @@ public class GlobalExceptionHandlerResolver {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* AccessDeniedException
|
||||
*
|
||||
@@ -113,7 +88,7 @@ public class GlobalExceptionHandlerResolver {
|
||||
/**
|
||||
* validation Exception
|
||||
*
|
||||
* @param exception
|
||||
* @param exception e
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
||||
|
||||
@@ -13,4 +13,10 @@ public class GlobalConstants {
|
||||
* 但在有唯一索引的情况下,会导致索引冲突,所以用 0 标识存活, 已删除数据记录为删除时间戳
|
||||
*/
|
||||
public static final Long NOT_DELETED_FLAG = 0L;
|
||||
|
||||
|
||||
/**
|
||||
* 生产环境
|
||||
*/
|
||||
public final static String ENV_PROD = "prod";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user