zoukankan      html  css  js  c++  java
  • 【Tips】这是一份多年来的异常catch经验

    package com.hb.merchant.controller;
    
    import com.hb.merchant.constant.MerchantConstant;
    import com.hb.merchant.exception.AccountException;
    import com.hb.util.enums.ResponseCode;
    import com.hb.util.exception.BusinessException;
    import com.hb.util.exception.ParamException;
    import com.hb.util.utils.ResponseData;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.validation.BindException;
    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.multipart.MaxUploadSizeExceededException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.validation.ConstraintViolationException;
    import java.util.Objects;
    
    /**
     * @author qucheng
     */
    @RestControllerAdvice
    @Slf4j
    public class ErrorController {
    
    
        private final static String SPLIT = "  <_>";
    
        @ExceptionHandler(MissingServletRequestParameterException.class)
        public ResponseData<Object> paramErrorHandler(Exception e, HttpServletRequest request) {
            log.error("url:{} -> Account参数异常:{}", request == null ? null : request.getRequestURL(), e);
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), e.getMessage());
        }
    
        @ExceptionHandler(MethodArgumentTypeMismatchException.class)
        public ResponseData<Object> mismatchErrorHandler(MethodArgumentTypeMismatchException e) {
            log.error("参数转换失败,方法:" + Objects.requireNonNull(e.getParameter().getMethod()).getName() + ",参数:" +
                    e.getName() + ",信息:" + e.getLocalizedMessage());
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), e.getMessage());
        }
    
        @ExceptionHandler(BusinessException.class)
        public ResponseData<Object> handler(BusinessException e, HttpServletRequest request) {
            log.error("url:{} -> 业务异常:{}", request == null ? null : request.getRequestURL(), e);
            return ResponseData.fail(e.getCode() == null ? ResponseCode.INTERNAL_ERROR.getCode() : e.getCode(), e.getMsg());
        }
    
        @ExceptionHandler(ParamException.class)
        public ResponseData<Object> handler(ParamException e, HttpServletRequest request) {
            log.error("url:{} -> 参数异常:{}", request == null ? null : request.getRequestURL(), e);
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), e.getMsg());
        }
    
        /**
         * 配合Assert使用,香
         *
         * @param e       e
         * @param request request
         * @return heiheihei
         */
        @ExceptionHandler({IllegalStateException.class, IllegalArgumentException.class})
        public ResponseData<Object> handler(RuntimeException e, HttpServletRequest request) {
            log.error("url:{} -> 业务异常:{}", request == null ? null : request.getRequestURL(), e);
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), e.getMessage());
        }
    
        @ExceptionHandler
        public ResponseData<Object> handler(Exception e, HttpServletRequest request) {
            log.error("url:{} -> 错误:{}", request == null ? null : request.getRequestURL(), e);
            return ResponseData.fail(ResponseCode.INTERNAL_ERROR.getCode(), MerchantConstant.ERROR_MSG_EXCEPTION);
        }
    
        /**
         * body参数
         *
         * @param e BindException
         */
        @ExceptionHandler(BindException.class)
        public ResponseData<Object> handlerBindException(BindException e) {
            StringBuilder msg = new StringBuilder();
            e.getBindingResult().getFieldErrors().forEach(c ->
                    msg.append(c.getDefaultMessage()).append(":").append(c.getRejectedValue()).append(SPLIT));
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), msg.substring(0, msg.lastIndexOf(SPLIT)));
        }
    
        /**
         * 方法参数
         *
         * @param e ConstraintViolationException
         */
        @ExceptionHandler(ConstraintViolationException.class)
        public ResponseData<Object> handlerConstraintViolationException(ConstraintViolationException e) {
            StringBuilder msg = new StringBuilder();
            e.getConstraintViolations().forEach(c ->
                    msg.append(c.getMessage()).append(":").append(c.getInvalidValue()).append(SPLIT));
            return ResponseData.fail(ResponseCode.PARAM_INVALIDATE.getCode(), msg.substring(0, msg.lastIndexOf(SPLIT)));
        }
    
    }
  • 相关阅读:
    Eclipse中一个Web项目引用另一个项目中的类
    android adb shell中使用到的命令
    移动端服务器i-jetty下载编译安装及问题解决系列
    Windows和Ubuntu双系统独立分区安装的方法
    Mina2.0框架源码剖析(三)
    Mina2.0框架源码剖析(二)
    Mina2.0框架源码剖析(一)
    JBoss
    J2EE的体系结构
    微博三方登录
  • 原文地址:https://www.cnblogs.com/nightOfStreet/p/13215709.html
Copyright © 2011-2022 走看看