🔄Update: 新增全局异常处理器
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package cn.hezhaohui.thumb.exception;
|
package cn.hezhaohui.thumb.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
public class BusinessException extends RuntimeException{
|
public class BusinessException extends RuntimeException{
|
||||||
|
|
||||||
private final int code;
|
private final int code;
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package cn.hezhaohui.thumb.exception;
|
||||||
|
|
||||||
|
import cn.hezhaohui.thumb.common.BaseResponse;
|
||||||
|
import cn.hezhaohui.thumb.common.ResultUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(BusinessException.class)
|
||||||
|
public BaseResponse<?> businessExceptionHandler(BusinessException e) {
|
||||||
|
log.error("BusinessException", e);
|
||||||
|
return ResultUtils.error(e.getCode(), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(RuntimeException.class)
|
||||||
|
public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
|
||||||
|
log.error("RuntimeException", e);
|
||||||
|
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user