GlobalExceptionHandler.java 4.16 KB
package com.subsidy.common.exception;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.subsidy.common.ResponseData;
import com.subsidy.common.ResponseVO;
import com.subsidy.common.configure.RemoteProperties;
import com.subsidy.common.constant.Code;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 异常处理
 * @author DengMin
 * @date 2020/07/14
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpException.class)
    public ResponseVO<T> handlerException(HttpServletRequest request, HttpException e) {
        String message = RemoteProperties.getMessage(e.getCode());
        if(StringUtils.isBlank(message)) {
            message = e.getMessage();
        }
        String url = request.getRequestURI();
        return ResponseData.generateCreatedResponse(e.getCode(), message, url);
    }

    /**
     * 404
     * @param request
     * @return
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public ResponseVO<T> NoHandlerFoundException(HttpServletRequest request) {
        String url = request.getRequestURI();
        return ResponseData.generateCreatedResponse(Code.NOHANDLERFOUND.getCode(), Code.NOHANDLERFOUND.getMessage(), url);
    }

    /**
     * 请求方式错误
     * @param request
     * @return
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ResponseVO<T> HttpRequestMethodNotSupportedException(HttpServletRequest request) {
        String url = request.getRequestURI();
        return ResponseData.generateCreatedResponse(Code.NOHANDLERFOUND.getCode(), Code.NOHANDLERFOUND.getMessage(), url);
    }

    /**
     * 参数不合法
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseVO<T> validateException(MethodArgumentNotValidException e){
        final List<String> errList = new ArrayList<>();
        e.getBindingResult().getAllErrors().stream().forEach(x -> {
            errList.add(x.getDefaultMessage());
        });
        return ResponseData.generateCreatedResponse(Code.PARAM_INVALID.getCode(), Code.PARAM_INVALID.getMessage(), errList.toString());
    }

    /**
     * JSON 序列化异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseVO<T> exceptionHandler(HttpMessageNotReadableException e) {
        log.error(e.getMessage());
        return ResponseData.generateCreatedResponse(Code.PARAM_INVALID.getCode(), Code.PARAM_INVALID.getMessage()+":{"+e.getMessage()+"}");
    }

    /**
     * 校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ResponseVO<T> BindException(BindException e){
        final List<String> errList = new ArrayList<>();
        e.getBindingResult().getAllErrors().stream().forEach(x -> {
            errList.add(x.getDefaultMessage());
        });
        return ResponseData.generateCreatedResponse(Code.PARAM_INVALID.getCode(), Code.PARAM_INVALID.getMessage(), errList.toString());
    }

    /**
     * 服务器内部错误
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseVO<T> serverInternalError(Exception e, HttpServletRequest request) {
        String url = request.getRequestURI();
        log.error("path:"+url);
        log.error("---- error message: ---"+e.toString());
        return ResponseData.generateCreatedResponse(Code.SERVER_INTERNAL_ERROR.getCode(), Code.SERVER_INTERNAL_ERROR.getMessage(), url);
    }
}