苍穹外卖--新增员工功能完善一-系统级别异常处理
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;
|
||||
|
||||
/**
|
||||
* 全局异常处理器,处理项目中抛出的业务异常
|
||||
*/
|
||||
@@ -24,4 +27,28 @@ public class GlobalExceptionHandler {
|
||||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 捕获系统级的数据库异常
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler
|
||||
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
|
||||
//Duplicate entry 'lisi' for key 'idx_username'
|
||||
String message = ex.getMessage();
|
||||
log.error("异常信息:{}",message );
|
||||
//动态获取错误数据
|
||||
if (message.contains("Duplicate entry")) {
|
||||
String[] msgs = message.split(" ");
|
||||
String username = msgs[2];
|
||||
// String msg = username + "已经存在";
|
||||
String msg = username + MessageConstant.ALEADY_EXIST;
|
||||
return Result.error(msg);
|
||||
}
|
||||
|
||||
// return Result.error(ex.getMessage());
|
||||
return Result.error(MessageConstant.UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user