feat(#N/A): 优化全局异常处理

This commit is contained in:
2025-08-06 18:33:36 +08:00
parent 32e0013673
commit 6b7c695f6f
2 changed files with 42 additions and 36 deletions
@@ -1,57 +1,62 @@
package com.guo.learningprogresstracker.common;
import cn.dev33.satoken.exception.NotLoginException;
import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author guo
*/
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
public class GlobalExceptionHandler {
@ExceptionHandler({ErrorParameterException.class})
@ResponseBody
public CommonResult errorParameterException(ErrorParameterException ex){
public CommonResult errorParameterException(ErrorParameterException ex) {
//code:400
return CommonResult.error(ex.getMessage());
}
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({NotFindEntitiesException.class})
@ResponseBody
public CommonResult notFindEntitiesException(NotFindEntitiesException ex){
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) {
//code:400
return CommonResult.error(ex.getMessage());
}
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({AppException.class})
@ResponseBody
public CommonResult AppException(AppException ex){
public CommonResult AppException(AppException ex) {
//code:500
return CommonResult.serverError(ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex){
BindingResult bindingResult = ex.getBindingResult();
return CommonResult.serverError(ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
//code:400
return CommonResult.error(bindingResult.getFieldError().getDefaultMessage());
return CommonResult.error(bindingResult.getFieldError().getDefaultMessage());
}
@ExceptionHandler(NotLoginException.class)
public CommonResult handleNotLogin(NotLoginException e, HttpServletRequest req, HttpServletResponse res) {
//code:401
res.setStatus(HttpStatus.UNAUTHORIZED.value());
return new CommonResult<>(HttpStatus.UNAUTHORIZED.value(), "未登录,请重新登录", null);
}
@ExceptionHandler(Exception.class)
@ResponseBody
public CommonResult Exception(Exception ex){
log.error("系统异常",ex);
public CommonResult Exception(Exception ex) {
log.error("系统异常", ex);
//code:400
return CommonResult.error(ex.getMessage());
}