外卖项目day01-新增员工_代码完善-统一异常响应结果Result
This commit is contained in:
@@ -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;
|
||||
|
||||
/**
|
||||
* 全局异常处理器,处理项目中抛出的业务异常
|
||||
*/
|
||||
@@ -19,9 +22,18 @@ public class GlobalExceptionHandler {
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler
|
||||
public Result exceptionHandler(BaseException ex){
|
||||
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
|
||||
log.error("异常信息:{}", ex.getMessage());
|
||||
return Result.error(ex.getMessage());
|
||||
//Duplicate entry '222' for key 'employee.idx_username'
|
||||
String message = ex.getMessage();
|
||||
if (message.contains("Duplicate entry")) {
|
||||
String[] split = message.split(" ");
|
||||
String username = split[2];
|
||||
String errorMsg = username + MessageConstant.ALEADY_EXISTS;
|
||||
return Result.error(errorMsg);
|
||||
}
|
||||
|
||||
return Result.error(MessageConstant.UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user