feat(#N/A): 优化全局异常处理
This commit is contained in:
@@ -1,55 +1,60 @@
|
|||||||
package com.guo.learningprogresstracker.common;
|
package com.guo.learningprogresstracker.common;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.exception.NotLoginException;
|
||||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||||
import com.guo.learningprogresstracker.exception.AppException;
|
import com.guo.learningprogresstracker.exception.AppException;
|
||||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||||
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
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.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author guo
|
* @author guo
|
||||||
*/
|
*/
|
||||||
@ControllerAdvice
|
@RestControllerAdvice
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler({ErrorParameterException.class})
|
@ExceptionHandler({ErrorParameterException.class})
|
||||||
@ResponseBody
|
|
||||||
public CommonResult errorParameterException(ErrorParameterException ex) {
|
public CommonResult errorParameterException(ErrorParameterException ex) {
|
||||||
//code:400
|
//code:400
|
||||||
return CommonResult.error(ex.getMessage());
|
return CommonResult.error(ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler({NotFindEntitiesException.class})
|
@ExceptionHandler({NotFindEntitiesException.class})
|
||||||
@ResponseBody
|
|
||||||
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) {
|
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) {
|
||||||
//code:400
|
//code:400
|
||||||
return CommonResult.error(ex.getMessage());
|
return CommonResult.error(ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler({AppException.class})
|
@ExceptionHandler({AppException.class})
|
||||||
@ResponseBody
|
|
||||||
public CommonResult AppException(AppException ex) {
|
public CommonResult AppException(AppException ex) {
|
||||||
//code:500
|
//code:500
|
||||||
return CommonResult.serverError(ex.getMessage());
|
return CommonResult.serverError(ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
@ResponseBody
|
|
||||||
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
|
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
|
||||||
BindingResult bindingResult = ex.getBindingResult();
|
BindingResult bindingResult = ex.getBindingResult();
|
||||||
//code:400
|
//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)
|
@ExceptionHandler(Exception.class)
|
||||||
@ResponseBody
|
|
||||||
public CommonResult Exception(Exception ex) {
|
public CommonResult Exception(Exception ex) {
|
||||||
log.error("系统异常", ex);
|
log.error("系统异常", ex);
|
||||||
//code:400
|
//code:400
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.entity;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@@ -17,34 +18,34 @@ public class CommonResult<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> CommonResult<T> success() {
|
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) {
|
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) {
|
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) {
|
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) {
|
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() {
|
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) {
|
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) {
|
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