feat(#N/A): 优化全局异常处理
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.entity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@@ -17,34 +18,34 @@ public class CommonResult<T> {
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success() {
|
||||
return new CommonResult<>(200, "请求成功", null);
|
||||
return new CommonResult<>(HttpStatus.OK.value(), "请求成功", null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success(String message) {
|
||||
return new CommonResult<>(200, message, null);
|
||||
return new CommonResult<>(HttpStatus.OK.value(), message, null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success(T data) {
|
||||
return new CommonResult<>(200, "请求成功", data);
|
||||
return new CommonResult<>(HttpStatus.OK.value(), "请求成功", data);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success(String message, T data) {
|
||||
return new CommonResult<>(200, message, data);
|
||||
return new CommonResult<>(HttpStatus.OK.value(), message, data);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> error(String message) {
|
||||
return new CommonResult<>(400, message, null);
|
||||
return new CommonResult<>(HttpStatus.BAD_REQUEST.value(), message, null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> notFind() {
|
||||
return new CommonResult<>(404, "请求的资源不存在", null);
|
||||
return new CommonResult<>(HttpStatus.NOT_FOUND.value(), "请求的资源不存在", null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> notFind(String message) {
|
||||
return new CommonResult<>(404, message, null);
|
||||
return new CommonResult<>(HttpStatus.NOT_FOUND.value(), message, null);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> serverError(String message) {
|
||||
return new CommonResult<>(500, message, null);
|
||||
return new CommonResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user