package com.jcdm.framework.web.exception; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingPathVariableException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import com.jcdm.common.constant.HttpStatus; import com.jcdm.common.core.domain.AjaxResult; import com.jcdm.common.exception.DemoModeException; import com.jcdm.common.exception.ServiceException; import com.jcdm.common.utils.StringUtils; /** * 全局异常处ç†å™¨ * * @author jc */ @RestControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * æƒé™æ ¡éªŒå¼‚常 */ @ExceptionHandler(AccessDeniedException.class) public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地å€'{}',æƒé™æ ¡éªŒå¤±è´¥'{}'", requestURI, e.getMessage()); return AjaxResult.error(HttpStatus.FORBIDDEN, "没有æƒé™ï¼Œè¯·è”系管ç†å‘˜æŽˆæƒ"); } /** * 请求方å¼ä¸æ”¯æŒ */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地å€'{}',ä¸æ”¯æŒ'{}'请求", requestURI, e.getMethod()); return AjaxResult.error(e.getMessage()); } /** * 业务异常 */ @ExceptionHandler(ServiceException.class) public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) { log.error(e.getMessage(), e); Integer code = e.getCode(); return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); } /** * 请求路径ä¸ç¼ºå°‘必需的路径å˜é‡ */ @ExceptionHandler(MissingPathVariableException.class) public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求路径ä¸ç¼ºå°‘必需的路径å˜é‡'{}',å‘生系统异常.", requestURI, e); return AjaxResult.error(String.format("请求路径ä¸ç¼ºå°‘必需的路径å˜é‡[%s]", e.getVariableName())); } /** * 请求å‚数类型ä¸åŒ¹é… */ @ExceptionHandler(MethodArgumentTypeMismatchException.class) public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求å‚数类型ä¸åŒ¹é…'{}',å‘生系统异常.", requestURI, e); return AjaxResult.error(String.format("请求å‚数类型ä¸åŒ¹é…,å‚æ•°[%s]è¦æ±‚类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); } /** * 拦截未知的è¿è¡Œæ—¶å¼‚常 */ @ExceptionHandler(RuntimeException.class) public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地å€'{}',å‘生未知异常.", requestURI, e); return AjaxResult.error(e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) public AjaxResult handleException(Exception e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地å€'{}',å‘生系统异常.", requestURI, e); return AjaxResult.error(e.getMessage()); } /** * 自定义验è¯å¼‚常 */ @ExceptionHandler(BindException.class) public AjaxResult handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return AjaxResult.error(message); } /** * 自定义验è¯å¼‚常 */ @ExceptionHandler(MethodArgumentNotValidException.class) public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return AjaxResult.error(message); } /** * 演示模å¼å¼‚常 */ @ExceptionHandler(DemoModeException.class) public AjaxResult handleDemoModeException(DemoModeException e) { return AjaxResult.error("演示模å¼ï¼Œä¸å…许æ“作"); } }