package com.poyee.common.exception; import com.poyee.base.dto.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.BindException; import org.springframework.validation.ObjectError; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.sql.SQLException; import java.util.List; /** * */ @ControllerAdvice @ResponseBody public class GlobalExceptionHandler extends RuntimeException { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面 */ /*@ExceptionHandler(AuthorizationException.class) public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e) { log.error(e.getMessage(), e); if (ServletUtils.isAjaxRequest(request)) { return AjaxResult.error(PermissionUtils.getMsg(e.getMessage())); } else { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("error/unauth"); return modelAndView; } }*/ /** * 请求方式不支持 */ @ExceptionHandler({ HttpRequestMethodNotSupportedException.class }) public Result handleException(HttpRequestMethodNotSupportedException e) { log.error("运行异常:{}", e); return Result.error("不支持' " + e.getMethod() + "'请求"); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) public Object notFount(RuntimeException e) { log.error("运行时异常:{}", e); if(e.getCause() instanceof SQLException){ return Result.error("sql运行异常,请联系管理员"); } return Result.error("运行出错了:" + e.getMessage()); } @ExceptionHandler(ServiceException.class) public Result serviceErr(ServiceException e) { log.error("运行时异常:{}", e); if(e.getCode() == 0){ e.code = -1; } return Result.error(e.getCode(),e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) public Result handleException(Exception e) { log.error("运行异常:{}", e); return Result.error("服务器错误,请联系管理员"); } /** * 业务异常 */ @ExceptionHandler(BaseException.class) public Object baseException(BaseException e) { log.error(e.getMessage(), e); String errMsg = e.getMessage(); return Result.error(errMsg); } /** * 业务异常 */ @ExceptionHandler(BusinessException.class) public Object businessException( BusinessException e) { log.error(e.getMessage(), e); String errMsg = e.getMessage(); Integer code = e.getCode(); return Result.error(code,errMsg); } /** * 自定义验证异常 */ @ExceptionHandler(BindException.class) public Result validatedBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return Result.error(message); } @ExceptionHandler(MethodArgumentNotValidException.class) public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { StringBuilder msg = new StringBuilder(); List allErrors = e.getBindingResult().getAllErrors(); for (ObjectError allError : allErrors) { msg.append(allError.getDefaultMessage()).append(","); } if (null != msg && msg.length()>1) { return Result.error(msg.substring(0,msg.length()-1)); } return Result.error(msg.toString()); } }