苍穹外卖--新增员工功能完善一-系统级别异常处理
This commit is contained in:
@@ -6,6 +6,7 @@ package com.sky.constant;
|
|||||||
public class MessageConstant {
|
public class MessageConstant {
|
||||||
|
|
||||||
public static final String PASSWORD_ERROR = "密码错误";
|
public static final String PASSWORD_ERROR = "密码错误";
|
||||||
|
public static final String ALEADY_EXIST = "已经存在";
|
||||||
public static final String ACCOUNT_NOT_FOUND = "账号不存在";
|
public static final String ACCOUNT_NOT_FOUND = "账号不存在";
|
||||||
public static final String ACCOUNT_LOCKED = "账号被锁定";
|
public static final String ACCOUNT_LOCKED = "账号被锁定";
|
||||||
public static final String UNKNOWN_ERROR = "未知错误";
|
public static final String UNKNOWN_ERROR = "未知错误";
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package com.sky.handler;
|
package com.sky.handler;
|
||||||
|
|
||||||
|
import com.sky.constant.MessageConstant;
|
||||||
import com.sky.exception.BaseException;
|
import com.sky.exception.BaseException;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.sql.SQLIntegrityConstraintViolationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局异常处理器,处理项目中抛出的业务异常
|
* 全局异常处理器,处理项目中抛出的业务异常
|
||||||
*/
|
*/
|
||||||
@@ -24,4 +27,28 @@ public class GlobalExceptionHandler {
|
|||||||
return Result.error(ex.getMessage());
|
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