• 分布式系统工具类


    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());
    }


    }




    小蚊子大人
  • 相关阅读:
    .NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记
    《ASP.NET Core 与 RESTful API 开发实战》-- (第10章)-- 读书笔记
    《ASP.NET Core 与 RESTful API 开发实战》-- (第9章)-- 读书笔记(下)
    《ASP.NET Core 与 RESTful API 开发实战》-- (第9章)-- 读书笔记(上)
    《ASP.NET Core 与 RESTful API 开发实战》-- (第8章)-- 读书笔记(尾)
    解决win10桌面图标变成白图标的问题
    微信小程序封装http请求
    Java如何控制某个方法允许并发访问的线程个数----Semaphore
    java实现获取用户IP地址
    shell有减号开头的参数的问题
  • 原文地址:https://www.cnblogs.com/ywsheng/p/14976062.html
Copyright © 2020-2023  润新知