package cn.besteam.common.exception; import cn.besteam.common.api.vo.Result; import cn.besteam.common.constant.ErrorCode; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.NoHandlerFoundException; import javax.servlet.http.HttpServletRequest; import java.security.InvalidParameterException; import java.sql.SQLException; import java.util.List; import java.util.Locale; /** * 异常处理器 * * @author scott * @date 2019 */ @RestControllerAdvice @Slf4j public class BusinessExceptionHandler { @ExceptionHandler(NoHandlerFoundException.class) public Result handlerNoFoundException(Exception e) { log.error(e.getMessage(), e); return Result.error(404, "路径不存在,请检查路径是否正确"); } @ExceptionHandler(BusinessException.class) public Result handlerBusinessException(BusinessException e) { log.error(e.getMessage(), e); return Result.error(e.code, e.getMessage()); } @ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseBody public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception, HttpServletRequest request) { log.info("不支持该请求方式异常", exception); ErrorCode error = ErrorCode.SYSTEM_HTTP_METHOD_ERROR; String message = error.msg; return Result.error(error.code, exception.getMessage()); } /** * 处理请求参数验证异常 * * @param exception * @param request * @return */ @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException exception, HttpServletRequest request) { log.info("处理请求参数验证异常", exception); ErrorCode error = ErrorCode.SYSTEM_REQUEST_PARAM_ERROR; String message = error.msg; BindingResult bindingResult = exception.getBindingResult(); if (bindingResult != null && bindingResult.hasErrors()) { List objectErrorList = bindingResult.getAllErrors(); if (!objectErrorList.isEmpty()) { message = objectErrorList.get(0).getDefaultMessage(); } } return Result.error(error.code, message); } /** * 处理请求参数验证异常 * * @param exception * @param request * @return */ @ExceptionHandler(BindException.class) @ResponseBody public Result handleBindException(BindException exception, HttpServletRequest request) { log.info("处理请求参数验证异常", exception); ErrorCode error = ErrorCode.SYSTEM_REQUEST_PARAM_ERROR; // String message = error.msg; // BindingResult bindingResult = exception.getBindingResult(); // if (bindingResult != null && bindingResult.hasErrors()) { // List objectErrorList = bindingResult.getAllErrors(); // if (!objectErrorList.isEmpty()) { // message = objectErrorList.get(0).getDefaultMessage(); // } // } return Result.error(error); } /** * 处理客户端请求参数错误 * * @param request 请求对象 * @param exception 异常对象 * @param locale 地理信息 * @return */ @ExceptionHandler({InvalidParameterException.class, ServletRequestBindingException.class, MissingServletRequestParameterException.class, HttpMessageNotReadableException.class}) @ResponseBody public Result handleArgumentException(HttpServletRequest request, Exception exception, Locale locale) { log.info("处理客户端请求参数错误", exception); return Result.error(ErrorCode.SYSTEM_REQUEST_PARAM_ERROR); } // 5xx--------------------------------------------------------- /** * 处理服务器端数据访问错误 * * @param request 请求对象 * @param exception 异常对象 * @param locale 地理信息 * @return */ @ExceptionHandler({SQLException.class, DataAccessException.class, DataAccessResourceFailureException.class, DataIntegrityViolationException.class}) @ResponseBody public Result handleSQLException(HttpServletRequest request, Exception exception, Locale locale) { log.error("SQL exception", exception); return Result.error(ErrorCode.ERR_500); } @ExceptionHandler(Exception.class) public Result handleException(Exception e) { log.error(e.getMessage(), e); return Result.error(ErrorCode.ERR_600); } }