🔄Update: 新增全局异常处理器

This commit is contained in:
2025-10-18 14:35:38 +08:00
parent 6d870cb817
commit eba78157d9
2 changed files with 27 additions and 0 deletions
@@ -1,5 +1,8 @@
package cn.hezhaohui.thumb.exception;
import lombok.Getter;
@Getter
public class BusinessException extends RuntimeException{
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, "系统错误");
}
}