feat:全局异常捕获

This commit is contained in:
xiang
2025-08-24 16:11:38 +08:00
parent c3ef7a4fe6
commit 27cde384a0
6 changed files with 136 additions and 2 deletions

View File

@@ -26,8 +26,12 @@ public class Result<T> {
public static Result<Void> success(String message) {
return new Result<Void>("200", message, null);
}
public static <T> Result<T> success(T data) {
return new Result<T>("200", "success", Collections.singletonList(data));
}
public static <T> Result<T> success(List<T> data) {
return new Result<T>("200", "success", data);
}
public static <T> Result<T> success(String message, List<T> data) {
return new Result<T>("200", message, data);
}

View File

@@ -0,0 +1,17 @@
package com.xiang.xservice.basic.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum DelStatusEnum {
NOT_DELETED(0, "未删除"),
DELETED(1, "已删除"),
;
private final Integer code;
private final String msg;
}

View File

@@ -0,0 +1,26 @@
package com.xiang.xservice.basic.exception;
import com.xiang.xservice.basic.exception.code.BaseErrorCode;
import com.xiang.xservice.basic.exception.code.ErrorCode;
import lombok.Getter;
@Getter
public class BusinessException extends RuntimeException {
private final String code;
public BusinessException(String code, String message) {
super(message);
this.code = code;
}
public BusinessException(String message) {
super(message);
this.code = ErrorCode.ERROR.getCode();
}
public BusinessException(BaseErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}
}

View File

@@ -0,0 +1,63 @@
package com.xiang.xservice.basic.exception;
import com.xiang.xservice.basic.common.resp.Result ;
import com.xiang.xservice.basic.exception.code.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 业务异常
*/
@ExceptionHandler(BusinessException.class)
public Result<Void> handleBusinessException(BusinessException e) {
log.warn("业务异常: {}", e.getMessage());
return Result.error(e.getCode(), e.getMessage());
}
/**
* 参数校验异常 - @Valid / @Validated
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<Void> handleValidException(MethodArgumentNotValidException e) {
String msg = e.getBindingResult().getFieldError() != null ?
e.getBindingResult().getFieldError().getDefaultMessage() : "参数错误";
log.warn("参数校验异常: {}", msg);
return Result.error(ErrorCode.PARAM_ERROR.getCode(), msg);
}
/**
* 参数绑定异常 - GET 请求参数校验失败
*/
@ExceptionHandler(BindException.class)
public Result<Void> handleBindException(BindException e) {
String msg = e.getFieldError() != null ? e.getFieldError().getDefaultMessage() : "参数错误";
log.warn("参数绑定异常: {}", msg);
return Result.error(ErrorCode.PARAM_ERROR.getCode(), msg);
}
/**
* JSON 解析异常
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public Result<Void> handleHttpMessageNotReadable(HttpMessageNotReadableException e) {
log.warn("JSON解析异常: {}", e.getMessage());
return Result.error(ErrorCode.JSON_ERROR.getCode(), ErrorCode.JSON_ERROR.getMessage());
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public Result<Void> handleException(Exception e) {
log.error("系统异常: ", e);
return Result.error(ErrorCode.ERROR.getCode(), ErrorCode.ERROR.getMessage());
}
}

View File

@@ -0,0 +1,7 @@
package com.xiang.xservice.basic.exception.code;
public interface BaseErrorCode {
String getCode();
String getMessage();
}

View File

@@ -0,0 +1,17 @@
package com.xiang.xservice.basic.exception.code;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ErrorCode implements BaseErrorCode {
ERROR("-1", "系统繁忙,请稍后再试!"),
PARAM_ERROR("-2", "请求参数异常!"),
JSON_ERROR("-3", "JSON异常!"),
;
private final String code;
private final String message;
}