处理SQL异常
This commit is contained in:
parent
cc6d1b8e5b
commit
4afb69f034
|
@ -24,4 +24,5 @@ public class MessageConstant {
|
|||
public static final String ORDER_STATUS_ERROR = "订单状态错误";
|
||||
public static final String ORDER_NOT_FOUND = "订单不存在";
|
||||
|
||||
public static final String ALREADY_EXISTS = "已存在了";
|
||||
}
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package com.sky.handler;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.exception.BaseException;
|
||||
import com.sky.result.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.sql.SQLIntegrityConstraintViolationException;
|
||||
|
||||
/**
|
||||
* 全局异常处理器,处理项目中抛出的业务异常
|
||||
*/
|
||||
|
@ -24,4 +27,21 @@ public class GlobalExceptionHandler {
|
|||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理SQL异常
|
||||
* @param exception SQLIntegrityConstraintViolationException
|
||||
* @return Result<String>
|
||||
*/
|
||||
@ExceptionHandler
|
||||
public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
|
||||
String message = exception.getMessage();
|
||||
if (message.contains("Duplicate entry")) {
|
||||
String[] split = message.split(" ");
|
||||
String username = split[2];
|
||||
String msg = username + MessageConstant.ALREADY_EXISTS;
|
||||
return Result.error(msg);
|
||||
} else {
|
||||
return Result.error(MessageConstant.UNKNOWN_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue