123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.futu.course.common.exception;
- import com.futu.course.common.constant.BestConstants;
- import com.futu.course.common.entity.R;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.jdbc.BadSqlGrammarException;
- import org.springframework.validation.BindException;
- 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
- public class GlobalExceptionHandler {
- private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
- /**
- * 拦截未知的运行时异常
- */
- @ExceptionHandler(RuntimeException.class)
- public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- log.error("请求地址'{}',发生未知异常.", requestURI, e);
- return R.failed(e.getMessage() == null ? "发生未知错误,请联系管理员!" : e.getMessage());
- }
- /**
- * 处理自定义异常
- */
- @ExceptionHandler(RRException.class)
- public R handleRRException(RRException e, HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- // log.error("请求地址'{}',发生系统异常.", requestURI, e);
- log.info("自定义异常");
- return R.failed(e.getCode(), e.getMsg());
- }
- /**
- * 系统异常
- */
- @ExceptionHandler(Exception.class)
- public R handleException(Exception e, HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- log.error("请求地址'{}',发生系统异常.", requestURI, e);
- return R.failed("系统错误,请联系管理员!");
- }
- /**
- * 系统异常
- */
- @ExceptionHandler(BadSqlGrammarException.class)
- public R handleBadSQLException(BadSqlGrammarException e, HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- log.error("请求地址'{}',发生系统异常.", requestURI, e);
- return R.failed("系统错误,请联系管理员!");
- }
- /**
- * 系统异常
- */
- @ExceptionHandler(SQLException.class)
- public R handleSQLException(SQLException e, HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- log.error("请求地址'{}',发生系统异常.", requestURI, e);
- return R.failed("系统错误,请联系管理员!");
- }
- /**
- * 自定义验证异常
- */
- @ExceptionHandler(BindException.class)
- public R handleBindException(BindException e) {
- // log.error(e.getMessage(), e);
- String message = e.getAllErrors().get(0).getDefaultMessage();
- return R.failed(message);
- }
- /**
- * 自定义验证异常
- */
- @ExceptionHandler(UnBindPhoneException.class)
- public R handleUnBindPhoneException(UnBindPhoneException e) {
- // log.error(e.getMessage(), e);
- return R.failed(BestConstants.USER_NOT_PHONE_CODE,BestConstants.USER_NOT_PHONE_MSG);
- }
- /**
- * 自定义验证异常
- */
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
- // log.error(e.getMessage(), e);
- log.info("自定义验证异常");
- String message = e.getBindingResult().getFieldError().getDefaultMessage();
- return R.failed(message);
- }
- }
|