package com.gkhy.exam.framework.exception; import com.gkhy.exam.common.api.CommonResult; import com.gkhy.exam.common.api.ResultCode; import com.gkhy.exam.common.exception.ApiException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; import java.sql.SQLException; /** * 全局异常处理类 */ @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @Autowired HttpServletRequest request; @ExceptionHandler(value = ApiException.class) public CommonResult handle(ApiException e){ writeExceptionLogFile(e); if(e.getErrorCode()!=null){ return CommonResult.failed(e.getErrorCode()); } return CommonResult.failed(e.getMessage()); } @ExceptionHandler(value = MethodArgumentNotValidException.class) public CommonResult handleValidException(MethodArgumentNotValidException e) { BindingResult bindingResult = e.getBindingResult(); String message = null; if (bindingResult.hasErrors()) { FieldError fieldError = bindingResult.getFieldError(); if (fieldError != null) { message = fieldError.getField()+fieldError.getDefaultMessage(); } } writeExceptionLogFile(e); return CommonResult.validateFailed(message); } @ExceptionHandler(value = BindException.class) public CommonResult handleValidException(BindException e) { BindingResult bindingResult = e.getBindingResult(); String message = null; if (bindingResult.hasErrors()) { FieldError fieldError = bindingResult.getFieldError(); if (fieldError != null) { message = fieldError.getField()+fieldError.getDefaultMessage(); } } writeExceptionLogFile(e); return CommonResult.validateFailed(message); } @ExceptionHandler(SQLException.class) public CommonResult sqlExceptionHandler(Exception ex) { writeExceptionLogFile(ex); return CommonResult.failed( "执行sql异常"); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) public CommonResult handleRuntimeException(RuntimeException ex, HttpServletRequest request) { writeExceptionLogFile(ex); return CommonResult.failed("内部服务异常"); } /** * 拦截Spring secrity AccessDenied 异常 */ @ExceptionHandler(AccessDeniedException.class) public CommonResult handleAccessDeniedException(RuntimeException ex, HttpServletRequest request) { writeExceptionLogFile(ex); return CommonResult.failed(ResultCode.FORBIDDEN,"权限不足,无法访问系统资源"); } /** * 500 其他服务器异常 */ @ExceptionHandler(Exception.class) public CommonResult otherExceptionHandler(Exception ex) { String message = !StringUtils.isEmpty(ex.getMessage()) ? ex.getMessage() : ex.toString(); if(StringUtils.isNotBlank(message)&&message.contains("java.sql")){ message="执行sql异常"; } writeExceptionLogFile(ex); return CommonResult.failed(message); } private void writeExceptionLogFile(Exception ex) { String url=""; if (request!=null) { url=request.getRequestURI(); } log.error("error={}",ex); log.error(ex.getMessage()+",url={}",url); } }