package com.chuanghai.attendance.common.exception; import com.alibaba.excel.exception.ExcelAnalysisException; import com.chuanghai.attendance.common.utils.CommonResult; import org.springframework.dao.DuplicateKeyException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.util.StringUtils; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingRequestHeaderException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.HashMap; import java.util.Map; /** * @Author: codingliang * @Description: 统一异常处理 * @Date: 2021-01-06 10:07 * @Version: V1.0 **/ @RestControllerAdvice public class MyExceptionHandler { /** * 处理参数校验异常 * @param e * @return */ @ExceptionHandler(MethodArgumentNotValidException.class) public CommonResult handleValidException(MethodArgumentNotValidException e) { Map map = new HashMap<>(); e.getBindingResult().getFieldErrors().forEach(item -> { String errMessage = item.getDefaultMessage(); String errField = item.getField(); map.put(errField, errMessage); }); return CommonResult.fail(Integer.toString(BizCodeEnume.VAILD_EXCEPTION.getCode()), BizCodeEnume.VAILD_EXCEPTION.getMsg()).setResult(map); } /** * 数据库索引重复 * @return */ @ExceptionHandler(DuplicateKeyException.class) public CommonResult handleSQLIntegrityConstraintViolationException() { return CommonResult.fail(Integer.toString(BizCodeEnume.DATA_IS_EXIST.getCode()), BizCodeEnume.DATA_IS_EXIST.getMsg()); } /** * 请求方法不支持 * @return */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public CommonResult handleRequestMethodNotSupported() { return CommonResult.fail(Integer.toString(BizCodeEnume.METHOD_NOT_SUPPORT.getCode()), BizCodeEnume.METHOD_NOT_SUPPORT.getMsg()); } /** * 缺少请求头 * @return */ @ExceptionHandler(MissingRequestHeaderException.class) public CommonResult handleRequestHeaderMissing() { return CommonResult.fail(Integer.toString(BizCodeEnume.REQUEST_HEADER_MISSING.getCode()), BizCodeEnume.REQUEST_HEADER_MISSING.getMsg()); } /** * 参数缺失 * @return */ @ExceptionHandler(MissingServletRequestParameterException.class) public CommonResult handleMissingServletRequestParameterException() { return CommonResult.fail(Integer.toString(BizCodeEnume.PARAMETER_ERROR.getCode()), BizCodeEnume.PARAMETER_ERROR.getMsg()); } /** * body为空 * @return */ @ExceptionHandler(HttpMessageNotReadableException.class) public CommonResult handleMessageNotReadableException() { return CommonResult.fail(Integer.toString(BizCodeEnume.BODY_IS_EMPTY.getCode()), BizCodeEnume.BODY_IS_EMPTY.getMsg()); } /** * easyExcel 文件读取失败 * @return */ @ExceptionHandler(ExcelAnalysisException.class) public CommonResult handleExcelAnalysisException(ExcelAnalysisException e) { String errorMessage = e.getMessage(); System.out.println(errorMessage); String errMsg = "文件导入失败"; if (StringUtils.hasText(errorMessage) && errorMessage.contains("doesn't have a default value")) { errMsg = "文件导入失败,请检查模板header是否正确"; } else if (StringUtils.hasText(errorMessage) && errorMessage.contains("Duplicate entity")) { errMsg = "文件导入失败,含有重复数据"; }else if (StringUtils.hasText(errorMessage) && errorMessage.contains("数据已存在") ){ errMsg = "文件导入失败,含有重复数据"; } return CommonResult.fail(Integer.toString(BizCodeEnume.FILE_IMPORT_ERROR.getCode()), errMsg); } /** * 自定义异常 * @param e * @return */ @ExceptionHandler(RRException.class) public CommonResult handleRRException(RRException e) { return CommonResult.fail(Integer.toString(e.getCode()), e.getMsg()); } /** * 未知异常 * @param throwable * @return */ @ExceptionHandler(Throwable.class) public CommonResult handleException(Throwable throwable) { throwable.printStackTrace(); return CommonResult.fail(Integer.toString(BizCodeEnume.UNKNOW_EXCEPTION.getCode()), BizCodeEnume.UNKNOW_EXCEPTION.getMsg()); } }