• 分布式系统工具类


    package com.box.common.core.exception;

    import com.box.common.core.enums.ErrorCodeEnum;
    import com.box.common.core.rt.Result;
    import com.box.common.core.utils.StringUtils;
    import feign.FeignException;
    import feign.RetryableException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.converter.HttpMessageNotReadableException;
    import org.springframework.validation.BindingResult;
    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.servlet.NoHandlerFoundException;
    import org.springframework.web.util.NestedServletException;

    import javax.servlet.http.HttpServletRequest;
    import javax.validation.ConstraintViolationException;
    import java.util.Objects;

    @RestControllerAdvice
    public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(value = OrderException.class)
    public Result orderExceptionHandler(HttpServletRequest req, OrderException e) {
    logger.info("订单异常!", e);
    return Result.fail(e.getCode(), e.getMsg());
    }

    /**
    * 处理自定义的业务异常
    *
    * @param req
    * @param e
    * @return
    */
    @ExceptionHandler(value = AuthException.class)
    public Result authExceptionHandler(HttpServletRequest req, AuthException e) {
    logger.info("用户登陆验证异常!", e);
    return Result.fail(e.getCode(), e.getMsg());
    }


    /**
    * 处理feign ok http 请求错误
    *
    * @param req
    * @param e
    * @return
    */
    @ExceptionHandler(value = RetryableException.class)
    public Result feignServiceExceptionHandler(HttpServletRequest req, RetryableException e) {
    logger.error("Feign 调用接口发生业务异常!", e);
    if(e.getCause() instanceof FeignServiceException){
    FeignServiceException feignServiceException = (FeignServiceException) e.getCause();
    return Result.fail(feignServiceException.getCode(), feignServiceException.getMsg());
    }
    return Result.fail(ErrorCodeEnum.SERVER.getCode(), ErrorCodeEnum.SERVER.getMessage());

    }


    /**
    * 处理自定义的业务异常
    *
    * @param req
    * @param e
    * @return
    */
    @ExceptionHandler(value = ServiceException.class)
    public Result mallServiceExceptionHandler(HttpServletRequest req, ServiceException e) {
    logger.error("服务端发生业务异常!", e);
    return Result.fail(e.getCode(), e.getMessage());
    }


    /**
    * 处理空指针的异常
    *
    * @param req
    * @param e
    * @return
    */
    @ExceptionHandler(value = NullPointerException.class)
    public Result exceptionHandler(HttpServletRequest req, NullPointerException e) {
    logger.error("发生空指针异常!", e);
    return Result.fail(e.getMessage());
    }

    /**
    * 处理其他异常
    *
    * @param req
    * @param e
    * @return
    */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(HttpServletRequest req, Exception e) {
    logger.error("未知异常!", e);
    return Result.fail(ErrorCodeEnum.FAIL.getMessage());
    }

    /**
    * 参数验证
    *
    * @return
    */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result constraintViolationExceptionHandle(ConstraintViolationException e) {
    //login.password: 请输入密码, login.account: 请输入账号
    String[] arrays = e.getMessage().split(",");
    StringBuffer msg = new StringBuffer();
    for (String array : arrays) {
    msg.append(array.split(":")[1]).append(",");
    }
    logger.info("请求参数验证错误1:{}", e.getMessage());
    return Result.fail(ErrorCodeEnum.CHECK_PARAM.getCode(), msg.deleteCharAt(msg.length()-1).toString());
    }




    /**
    * 参数验证
    *
    * @return
    */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidExceptionHandle(MethodArgumentNotValidException e) {
    logger.info("请求参数验证错误2:{}", e.getMessage());
    BindingResult bindingResult = e.getBindingResult();
    String msg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
    return Result.fail(ErrorCodeEnum.CHECK_PARAM.getCode(), msg);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result httpRequestMethodNotSupportedExceptionHandle(HttpRequestMethodNotSupportedException e) {
    logger.error("请求方式错误", e);
    return Result.fail(ErrorCodeEnum.REQUEST_NOT_METHOD.getCode(),e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result noHandlerFoundExceptionExceptionHandle(NoHandlerFoundException e) {
    logger.error("请求方式错误", e);
    return Result.fail(ErrorCodeEnum.REQUEST_NOT_METHOD.getCode(),e.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result httpMessageNotReadableExceptionHandle(HttpMessageNotReadableException e) {
    logger.error("数据格式转换错误", e);
    return Result.fail(ErrorCodeEnum.PARAM_CONVERSION_ERROR.getCode(),ErrorCodeEnum.PARAM_CONVERSION_ERROR.getMessage());
    }


    }




    小蚊子大人
  • 相关阅读:
    定义serialVersionUID的作用与意义整理
    HttpClient学习整理
    Eclipse+TestNG搭建接口自动化测试框架
    Jmeter之Bean shell使用(一)
    吴军博士的《数学之美》(摘录)
    SqlServer—大话函数依赖与范式
    MySQL—FOREIGN KEY
    MYSQL-用户操作
    WAMPServer 默认安装启动后,图标显示橙黄色
    Linux time命令
  • 原文地址:https://www.cnblogs.com/ywsheng/p/14976062.html
Copyright © 2020-2023  润新知