Blame view

GlobalExceptionHandler.java 4.19 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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());
        e.printStackTrace();
        return ResponseData.generateCreatedResponse(Code.SERVER_INTERNAL_ERROR.getCode(), Code.SERVER_INTERNAL_ERROR.getMessage(), url);
    }
}