import com.xkr.lsos.common.Result;
import com.xkr.lsos.common.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ValidationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: 全局的的异常拦截器(拦截所有的控制器)
*/
@RestControllerAdvice
@Order(-1)
@Slf4j
public class GlobalExceptionHandler {
/**
* 所有异常信息
*
* @param exception
* @return
* @throws Exception
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
public Result<?> exceptionHandler(Exception exception) {
log.error("exceptionHandle [{}]", exception.toString());
if (exception instanceof LoginToException){
exception.printStackTrace();
return Result.error(((LoginToException) exception).getMessageCode(), exception.getMessage());
}
exception.printStackTrace();
return Result.error(ReturnCode.OTHER_ERROR.getCode(), exception.getMessage());
}
@ExceptionHandler(DuplicateKeyException.class)
public Result<?> handleDuplicateKeyException(DuplicateKeyException e) {
log.error(e.getMessage(), e);
return Result.error("数据库中已存在该记录");
}
@ExceptionHandler(AuthenticationException.class)
public Result<?> authenticationExceptionException(DuplicateKeyException e) {
log.error(e.getMessage(), e);
return Result.error("Token已失效");
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error(e.getMessage(), e);
List<Map<String, Object>> list = new ArrayList<>();
List<FieldError> errorList = e.getBindingResult().getFieldErrors();
errorList.forEach(fieldError -> {
Map<String, Object> map = new HashMap<>();
map.put(fieldError.getField(), fieldError.getDefaultMessage());
list.add(map);
});
return Result.error(HttpStatus.EXPECTATION_FAILED.value(), "参数异常", list);
}
//自定义校验报错
@ExceptionHandler(ValidationException.class)
public Result<?> validationException(ValidationException e) {
log.error(e.getMessage(), e);
return Result.error("参数异常");
}
}
/**
* @Description: 自定义异常
**/
public class DefinedException extends RuntimeException {
private static final long serialVersionUID = -420925980366951255L;
/**
* 异常编号
*/
private final int messageCode;
/**
* 对messageCode 异常信息进行补充说明
*/
private final String detailMessage;
public DefinedException(int messageCode, String message) {
super(message);
this.messageCode = messageCode;
this.detailMessage = message;
}
public DefinedException(String message) {
super(message);
this.messageCode = 5000;
this.detailMessage = message;
}
public int getMessageCode() {
return messageCode;
}
public String getDetailMessage() {
return detailMessage;
}
}
/**
* @Description: 登录相关异常
**/
public class LoginToException extends RuntimeException {
private static final long serialVersionUID = -2688474821224087715L;
/**
* 异常编号
*/
private final int messageCode;
/**
* 对messageCode 异常信息进行补充说明
*/
private final String detailMessage;
public LoginToException(int messageCode, String message) {
super(message);
this.messageCode = messageCode;
this.detailMessage = message;
}
public int getMessageCode() {
return messageCode;
}
public String getDetailMessage() {
return detailMessage;
}
}
使用案例:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192801.html