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<ObjectError> 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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值