All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
top.doudou.common.tool.exception.GlobalExceptionHandler Maven / Gradle / Ivy
package top.doudou.common.tool.exception;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import top.doudou.base.exception.CustomException;
import top.doudou.base.respond.Result;
import javax.validation.UnexpectedTypeException;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.List;
/**
* @author 傻男人<[email protected] >
* @Date: 2020/06/18 14:20
* @Version: 1.0
* @Description:
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public Result exceptionHandler(Exception e) {
e.printStackTrace();
return Result.error("内部服务出错了",e.getMessage());
}
@ExceptionHandler(CustomException.class)
@ResponseBody
public Result bizExceptionHandler(CustomException e) {
e.printStackTrace();
return Result.fail(e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Result exceptionHandler(RuntimeException e) {
e.printStackTrace();
return Result.error(e.getMessage(),e.getLocalizedMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ResponseBody
public Result httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
return Result.error(405,"不支持当前请求"+e.getMethod(),e);
}
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public Result nullPointerExceptionHandler(NullPointerException e) {
StackTraceElement element = e.getStackTrace()[0];
String errorMsg = element.getFileName()+"文件中的方法名为:"+element.getMethodName()+"的方法,该方法中行号: "+element.getLineNumber()+" 出现空指针";
log.error(errorMsg);
return Result.error("空指针异常",errorMsg);
}
// -------------------- 参数相关-------------------------------------
@ExceptionHandler(MultipartException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public Result multipartExceptionHandler(MultipartException e) {
return Result.error(1000,"请求中缺少Multipart",e);
}
@ExceptionHandler(MissingServletRequestPartException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public Result missingServletRequestPartExceptionHandler(MissingServletRequestPartException e) {
return Result.error(2000,"缺少"+e.getRequestPartName()+"参数",e);
}
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public Result missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
return Result.error(2000,"缺少"+e.getParameterName()+"参数",e);
}
/**
* 参数验证异常处理
* @param e
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Result methodArgumentNotValidExceptionHandle(MethodArgumentNotValidException e){
StringBuilder sb = new StringBuilder(100);
List list = e.getBindingResult().getFieldErrors();
for (FieldError fieldError : list) {
sb.append(fieldError.getField()).append(" ").append(fieldError.getDefaultMessage()).append(";");
}
return Result.error(400,"参数错误: "+sb.toString());
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public Result illegalArgumentExceptionHandler(IllegalArgumentException e) {
e.printStackTrace();
return Result.error(606,"参数非法,请检查参数格式",e);
}
@ExceptionHandler(UnexpectedTypeException.class)
@ResponseBody
public Result unexpectedTypeExceptionHandler(UnexpectedTypeException e) {
e.printStackTrace();
return Result.error(607,"注解与需要校验的数据类型不匹配",e);
}
/**
* 参数校验异常
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public Result bindExceptionHandler(BindException e) {
BindingResult bindingResult = ((BindException) e).getBindingResult();
if (null != bindingResult && bindingResult.hasErrors()) {
List jsonList = new ArrayList<>();
bindingResult.getFieldErrors().stream().forEach(fieldError -> {
jsonList.add("属性:"+fieldError.getField()+",错误的原因:"+fieldError.getDefaultMessage());
});
log.error("参数效验出错:{}", StringUtils.join(jsonList," "));
return Result.error(603,"参数错误",jsonList);
}
log.error("参数效验出错");
return Result.error(603,"参数错误");
}
@ExceptionHandler(HttpMessageConversionException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public Result httpMessageConversionExceptionHandler(HttpMessageConversionException e) {
e.printStackTrace();
return Result.error(602,"参数类型错误",e);
}
// -------------------- 数据库-------------------------------------
@ExceptionHandler(SQLSyntaxErrorException.class)
@ResponseBody
public Result sQLSyntaxErrorExceptionHandler(SQLSyntaxErrorException e) {
e.printStackTrace();
return Result.error(700,"数据库出错了",e);
}
@ExceptionHandler(DataAccessException.class)
@ResponseBody
public Result dataAccessExceptionHandler(DataAccessException e) {
e.printStackTrace();
return Result.error(701,"数据库访问异常",e);
}
@ExceptionHandler(SQLException.class)
@ResponseBody
public Result sqlExceptionHandler(SQLException e) {
e.printStackTrace();
return Result.error(702,"sql异常",e);
}
@ExceptionHandler(DuplicateKeyException.class)
@ResponseBody
public Result duplicateKeyExceptionHandler(DuplicateKeyException e) {
e.printStackTrace();
return Result.error(703,"数据库主键重复",e);
}
/**
* Insert或Update数据时违反了完整性,例如违反了惟一性限制
* @param e
* @return
*/
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseBody
public Result dataIntegrityViolationExceptionHandler(DataIntegrityViolationException e) {
e.printStackTrace();
return Result.error(704,"违反了完整性",e);
}
/**
* 数据访问资源彻底失败,例如不能连接数据库
* @param e
* @return
*/
@ExceptionHandler(DataAccessResourceFailureException.class)
@ResponseBody
public Result dataAccessResourceFailureExceptionHandler(DataAccessResourceFailureException e) {
e.printStackTrace();
return Result.error(705,"数据访问资源彻底失败",e);
}
/**
* 当前的操作因为死锁而失败
* @param e
* @return
*/
@ExceptionHandler(DeadlockLoserDataAccessException.class)
@ResponseBody
public Result dataAccessResourceFailureExceptionHandler(DeadlockLoserDataAccessException e) {
e.printStackTrace();
return Result.error(706,"操作因死锁而失败",e);
}
// -------------------- 转换出错-------------------------------------
@ExceptionHandler(ClassCastException.class)
@ResponseBody
public Result classCastExceptionHandler(Exception e) {
e.printStackTrace();
return Result.error(601,"引用类型转换异常",e);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public Result httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
e.printStackTrace();
return Result.error(601,"json序列化错误",e);
}
@ExceptionHandler(MismatchedInputException.class)
@ResponseBody
public Result mismatchedInputExceptionHandler(MismatchedInputException e) {
e.printStackTrace();
return Result.error(601,"json转换为实体类出错",e);
}
@ExceptionHandler(TypeMismatchException.class)
@ResponseBody
public Result typeMismatchExceptionHandler(TypeMismatchException e) {
e.printStackTrace();
return Result.error(603,"类型不匹配",e);
}
}