外卖项目day01-新增员工_代码完善-统一异常响应结果Result
This commit is contained in:
@@ -8,7 +8,7 @@ public class MessageConstant {
|
|||||||
public static final String PASSWORD_ERROR = "密码错误";
|
public static final String PASSWORD_ERROR = "密码错误";
|
||||||
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 = "未知错误,请联系管理员";
|
||||||
public static final String USER_NOT_LOGIN = "用户未登录";
|
public static final String USER_NOT_LOGIN = "用户未登录";
|
||||||
public static final String CATEGORY_BE_RELATED_BY_SETMEAL = "当前分类关联了套餐,不能删除";
|
public static final String CATEGORY_BE_RELATED_BY_SETMEAL = "当前分类关联了套餐,不能删除";
|
||||||
public static final String CATEGORY_BE_RELATED_BY_DISH = "当前分类关联了菜品,不能删除";
|
public static final String CATEGORY_BE_RELATED_BY_DISH = "当前分类关联了菜品,不能删除";
|
||||||
@@ -24,4 +24,5 @@ public class MessageConstant {
|
|||||||
public static final String ORDER_STATUS_ERROR = "订单状态错误";
|
public static final String ORDER_STATUS_ERROR = "订单状态错误";
|
||||||
public static final String ORDER_NOT_FOUND = "订单不存在";
|
public static final String ORDER_NOT_FOUND = "订单不存在";
|
||||||
|
|
||||||
|
public static final String ALEADY_EXISTS = "已经存在了";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局异常处理器,处理项目中抛出的业务异常
|
* 全局异常处理器,处理项目中抛出的业务异常
|
||||||
*/
|
*/
|
||||||
@@ -19,9 +22,18 @@ public class GlobalExceptionHandler {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
public Result exceptionHandler(BaseException ex){
|
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
|
||||||
log.error("异常信息:{}", ex.getMessage());
|
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