【改进】全局异常捕获
This commit is contained in:
@@ -5,10 +5,16 @@ import com.corewing.app.common.Result;
|
|||||||
import com.corewing.app.util.I18nUtil;
|
import com.corewing.app.util.I18nUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.ConstraintViolationException;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局异常处理器
|
* 全局异常处理器
|
||||||
*/
|
*/
|
||||||
@@ -46,14 +52,39 @@ public class GlobalExceptionHandler {
|
|||||||
return Result.error(HttpStatus.UNAUTHORIZED.value(), message);
|
return Result.error(HttpStatus.UNAUTHORIZED.value(), message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 @Valid 参数校验异常(RequestBody 参数)
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
|
// 获取第一个错误信息
|
||||||
|
String errorMessage = e.getBindingResult().getFieldErrors().stream()
|
||||||
|
.map(FieldError::getDefaultMessage)
|
||||||
|
.collect(Collectors.joining("; "));
|
||||||
|
return Result.error(HttpStatus.BAD_REQUEST.value(), errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 @Validated 参数校验异常(RequestParam/PathVariable 参数)
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(ConstraintViolationException.class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
public Result<String> handleConstraintViolationException(ConstraintViolationException e) {
|
||||||
|
// 获取第一个错误信息
|
||||||
|
String errorMessage = e.getConstraintViolations().stream()
|
||||||
|
.map(ConstraintViolation::getMessage)
|
||||||
|
.collect(Collectors.joining("; "));
|
||||||
|
return Result.error(HttpStatus.BAD_REQUEST.value(), errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理其他异常
|
* 处理其他异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
||||||
public Result<String> handleException(Exception e) {
|
public Result<String> handleException(Exception e) {
|
||||||
log.error(e.getMessage());
|
log.error("系统异常: ", e);
|
||||||
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
e.getMessage());
|
I18nUtil.getMessage("error.system"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ error.token.replaced=Account has been logged in elsewhere
|
|||||||
error.token.kicked.out=Account has been kicked offline
|
error.token.kicked.out=Account has been kicked offline
|
||||||
error.not.login=Not logged in, please login first
|
error.not.login=Not logged in, please login first
|
||||||
error.server.internal=Internal server error: {0}
|
error.server.internal=Internal server error: {0}
|
||||||
|
error.system=System error, please try again later
|
||||||
|
|
||||||
# ==================== Utility Classes ====================
|
# ==================== Utility Classes ====================
|
||||||
# Redis Utility
|
# Redis Utility
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ error.token.replaced=账号已在其他地方登录
|
|||||||
error.token.kicked.out=账号已被踢下线
|
error.token.kicked.out=账号已被踢下线
|
||||||
error.not.login=未登录,请先登录
|
error.not.login=未登录,请先登录
|
||||||
error.server.internal=服务器内部错误:{0}
|
error.server.internal=服务器内部错误:{0}
|
||||||
|
error.system=系统错误,请稍后重试
|
||||||
|
|
||||||
# ==================== 工具类 ====================
|
# ==================== 工具类 ====================
|
||||||
# Redis工具类
|
# Redis工具类
|
||||||
|
|||||||
Reference in New Issue
Block a user