1
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.aida.seller.common.exception;
|
||||
|
||||
import com.aida.seller.common.result.ResultEnum;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author: dwjian
|
||||
* @description: 业务异常
|
||||
*/
|
||||
@Data
|
||||
@Slf4j
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private Integer code;
|
||||
private String msg;
|
||||
|
||||
public BusinessException(ResultEnum resultEnum) {
|
||||
this.code = resultEnum.getCode();
|
||||
this.msg = resultEnum.getMsg();
|
||||
}
|
||||
|
||||
public BusinessException(String msg) {
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public BusinessException(String msg, Integer code) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public BusinessException(Throwable cause) {
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = cause.getMessage();
|
||||
}
|
||||
|
||||
public BusinessException(ResultEnum resultEnum, String customMsg) {
|
||||
this.code = resultEnum.getCode();
|
||||
this.msg = customMsg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.aida.seller.common.exception;
|
||||
|
||||
import com.aida.seller.common.result.Response;
|
||||
import com.aida.seller.common.result.ResultEnum;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public Response<?> handleBusinessException(BusinessException e) {
|
||||
log.error("业务异常: code={}, msg={}", e.getCode(), e.getMsg());
|
||||
return Response.fail(e.getCode(), e.getMsg());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Response<?> handleValidationException(MethodArgumentNotValidException e) {
|
||||
String message = e.getBindingResult().getFieldErrors().stream()
|
||||
.map(FieldError::getDefaultMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
log.error("参数校验异常: {}", message);
|
||||
return Response.fail(ResultEnum.PARAMETER_ERROR.getCode(), message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
public Response<?> handleBindException(BindException e) {
|
||||
String message = e.getBindingResult().getFieldErrors().stream()
|
||||
.map(FieldError::getDefaultMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
log.error("参数绑定异常: {}", message);
|
||||
return Response.fail(ResultEnum.PARAMETER_ERROR.getCode(), message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Response<?> handleException(Exception e) {
|
||||
log.error("系统异常: ", e);
|
||||
return Response.error("系统繁忙,请稍后再试");
|
||||
}
|
||||
/**
|
||||
* 处理MinIO异常
|
||||
*/
|
||||
@ExceptionHandler(MinioException.class)
|
||||
public ResponseEntity<Object> handleMinioException(MinioException e) {
|
||||
log.error("[MinioException] {}", e.getMessage(), e);
|
||||
String message = e.getMessage();
|
||||
if (message != null && (message.contains("文件不能为空") || message.contains("不能为空"))) {
|
||||
Response<?> response = Response.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "File cannot be empty");
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
Response<?> response = Response.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "File storage service error");
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.aida.seller.common.exception;
|
||||
|
||||
/**
|
||||
* MinIO 操作异常类
|
||||
* 用于处理 MinIO 相关的业务异常
|
||||
*
|
||||
* @author Aida
|
||||
* @since 2024-01-01
|
||||
*/
|
||||
public class MinioException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private String errorCode;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param message 异常信息
|
||||
*/
|
||||
public MinioException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param message 异常信息
|
||||
* @param cause 原因
|
||||
*/
|
||||
public MinioException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param errorCode 错误码
|
||||
* @param message 异常信息
|
||||
*/
|
||||
public MinioException(String errorCode, String message) {
|
||||
super(message);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param errorCode 错误码
|
||||
* @param message 异常信息
|
||||
* @param cause 原因
|
||||
*/
|
||||
public MinioException(String errorCode, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误码
|
||||
*
|
||||
* @return 错误码
|
||||
*/
|
||||
public String getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误码
|
||||
*
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public void setErrorCode(String errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user