SpringBoot全局异常处理类

package com.yanek.error;

import com.neo.common.CodeMsg;
import com.neo.common.Result;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestControllerAdvice
public class GlobalExceptionTranslator {

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

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result<String> handleError(MissingServletRequestParameterException e) {
        logger.error("Missing Request Parameter", e);
        String message = String.format("Missing Request Parameter: %s", e.getParameterName());
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public Result<String> handleError(MethodArgumentTypeMismatchException e) {
        logger.error("Method Argument Type Mismatch", e);
        String message = String.format("Method Argument Type Mismatch: %s", e.getName());
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleError(MethodArgumentNotValidException e) {
        logger.error("Method Argument Not Valid", e);
        BindingResult result = e.getBindingResult();
        FieldError error = result.getFieldError();
        String message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }

   


    /**
     * 统一处理 BindException 错误
     * @param ex 参数验证失败错误
     * @return 参数验证失败响应
     */
    @ExceptionHandler(BindException.class)
    public Result<String> processBindException(BindException ex, HttpSession httpSession, HttpServletRequest request) {
        // 获取错误信息
        logger.error("Bind Exception", ex);
        List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
        // 目前消息只返回一条错误信息,所以只需要取第一条错误信息即可
        Map<String, String> errorMap = null;
        if (fieldErrors.size() > 0) {
            errorMap = new HashMap<>(fieldErrors.size());
            for (FieldError fieldError : fieldErrors) {
                errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
        }
        // 请求路径
        String url = request.getRequestURI();
        logger.error("Bind Exception 请求sessionId:{},请求接口:{},请求参数:{}, 异常信息: {}", httpSession.getId(), url, errorMap, ex.getMessage(), ex);
        Result<String> ret = Result.error(CodeMsg.BIND_ERROR.fillArgs(errorMap));
        return ret;
        //return Result.wrap(10000, "参数校验异常", errorMap);
    }
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<String> handleError(NoHandlerFoundException e) {
        logger.error("404 Not Found", e);
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<String> handleError(HttpMessageNotReadableException e) {
        logger.error("Message Not Readable", e);
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<String> handleError(HttpRequestMethodNotSupportedException e) {
        logger.error("Request Method Not Supported", e);
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Result<String> handleError(HttpMediaTypeNotSupportedException e) {
        logger.error("Media Type Not Supported", e);
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }
    @ExceptionHandler(ConstraintViolationException.class)
    public Result<String> handleError(ConstraintViolationException e) {
        logger.warn("Constraint Violation", e);
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        ConstraintViolation<?> violation = violations.iterator().next();
        String path = ((PathImpl) violation.getPropertyPath()).getLeafNode().getName();
        String message = String.format("%s:%s", path, violation.getMessage());
        System.out.println("message::"+message);
        Result<String> ret = Result.error(CodeMsg.PARM_VALID_ERROR);
        return ret;
    }


    @ExceptionHandler(ServiceException.class)
    public Result<String> handleError(ServiceException e) {
        logger.error("Service Exception", e);
        ServiceException ex = (ServiceException)e;
        Result<String> ret = Result.error(ex.getCm());
        return ret;
    }
    @ExceptionHandler(Throwable.class)
    public Result<String> handleError(Throwable e) {
        logger.error("Internal Server Error", e);
        Result<String> ret = Result.error(CodeMsg.SERVER_ERROR);
        return ret;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值