springboot 之异常捕捉器

package com.**.app.exception;

import com.**.dubbo.rpc.RpcException;
import com.**.app.config.interceptor.ClientRequestContext;
import com.**.enums.ResultMsgEnum;
import com.**.exception.BusinessException;
import com.**.web.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

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

/**
* @date 2018-07-11 18:09
 * @description
 */
@RestControllerAdvice
@RestController
public class ExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseBean handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error("参数解析失败|requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("could_not_read_json", 400);
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseBean handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        logger.error("不支持当前请求方法|requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("request_method_not_supported", 405);
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseBean handleHttpMediaTypeNotSupportedException(Exception e) {
        logger.error("不支持当前媒体类型|requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("content_type_not_supported", 415);
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseBean handleConstraintViolationException(MethodArgumentNotValidException e) {
        logger.error("参数验证失败|requestInfo:{}", getRequestContextInfo(), e);
        List objectErrors = e.getBindingResult().getAllErrors();
        String message = objectErrors.iterator().next().getDefaultMessage();
        return new ResponseBean().failure(message, 400);
    }

    /**
     * 413 - Request Entity Too Large
     */
    @ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE)
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseBean handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        logger.error("参数验证失败,文件过大|requestInfo:{}", getRequestContextInfo(),e);
        return new ResponseBean().failure("file_maximum_upload_size_exceeded", 413);
    }

    /**
     * 200 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({IllegalArgumentException.class})
    public ResponseBean illegalArgumentException(Exception e) {
        logger.error("{}|requestInfo:{}", e.getClass().getName(), getRequestContextInfo(), e);
        String msg = e.getMessage();
        logger.error(msg, e);
        return new ResponseBean().failure(msg);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({BusinessException.class})
    public ResponseBean businessException(BusinessException e) {
        logger.error("业务异常|requestInfo:{}|error:{}", getRequestContextInfo(), Objects.isNull(e.getResultMsg()) ? e.getMessage() : e.getResultMsg().getName());
        String msg = e.getMessage();
        ResultMsgEnum resultMsg = e.getResultMsg();
        if(Objects.isNull(resultMsg)) {
            return new ResponseBean().failure(msg);
        }
        if(e.getErrorObj() instanceof String[]) {
            return new ResponseBean().failure(e.getResultMsg().getCode(), (String[]) e.getErrorObj());
        } else {
            return new ResponseBean().failure(e.getResultMsg().getCode());
        }
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({RpcException.class})
    public ResponseBean rpcException(RpcException e) {
        logger.error("RpcException|requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("common_failure_0001");
    }

    /**
     * 401 - Internal Server Error
     */
    @RequestMapping("/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseBean unauthorizedException() {
        return new ResponseBean().failure("user not login", 401);
    }

    /**
     * 500 - IIllegalStateException
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({ IllegalStateException.class})
    public ResponseBean illegalStateException(IllegalStateException e) {
        logger.error("程序运行异常, requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("common_failure_0001", 500);
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public ResponseBean handleException(Exception e) {
        logger.error("程序运行异常, requestInfo:{}", getRequestContextInfo(), e);
        return new ResponseBean().failure("common_failure_0001", 500);
    }

    private HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(requestAttributes)) {
            return null;
        }
        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    private String getRequestContextInfo() {
        HttpServletRequest request = getHttpServletRequest();
//可以存放一些请求路径,请求方式,请求IP的输出,以便异常定位
        return JSON.toJSONString(request.getRequestURL);
    }
}

你可能感兴趣的:(springboot)