• 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_18-异常处理-不可预知异常处理


    框架抛出来的或者一些第三方的组件抛出来的异常。我们根本不知道它所对应的错误代码的信息,所以我们也没有办法给用户返回具体的错误代码和错误信息。
    我们先在Map中定义有一些不可预知的异常,定义错误代码和错误信息。如果我们在map中找到了这个错误代码,我们可以给用户返回,如果我们没有找到。就返回统一的99999异常
    这种异常我们自定义的错误代码。例如返回操作失败,请与管理员联系这种。
    对于不可预知的异常我们这么去处理。

    首先模拟一下不可预知的异常。添加的方法,我们body里面不传json数据就调用后端方法

    这个时候返回了400错误信息,这不是我们统一的响应格式信息。


    这才是我们统一的响应信息


    要么把这类异常返回为99999。要么就把这些异常定义一个具体的代码

    首先在项目里面我们想把这类异常捕获到。在异常捕获类里面,这里@ExceptionHandler来捕获Exception.class这个类型的异常。
    但是这里我们获取不到异常的代码

    在这里加一个断点,我们看下异常的类型是什么

    断点进来。把这个捕获的异常信息复制出去。


    org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.xuecheng.framework.domain.cms.response.CmsPageResult com.xuecheng.manage_cms.controller.CmsPageController.add(com.xuecheng.framework.domain.cms.CmsPage)

    异常意思是请求的body 没有数据

    针对这类异常我们统一返回99999。就是CommCode这个枚举里面的Server_error

    例如这里我们返回99999

    
    
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception exception){
    //记录日志
    LOGGER.error("catch exception:{}",exception.getMessage());
    return new ResponseResult(CommonCode.SERVER_ERROR);
    }
     

    这样最终统一返回的数据如下

    Map中定义

    有些不可预知的异常是可以定义错误代码的


    我们可以对HttpMessageNotReadableException这个类型定义错误代码。

    在CommonCode定义非法参数的这个枚举

    INVALID_PARAM(false,10003,"非法参数"),


    ImmutableMap 是谷歌的工具包下的类型,这个map的数据一旦放进去是不可更改的。也就是只读的

    前面的表示异常类型,异常都是会继承Throwable。后面是异常代码


    再定义builder对象,builder对象是用来构建ImmutableMap的。注意修饰符是protected

    那么怎么去构建呢?放在静态代码块里面。现在这个数据还在这个builder里面 ,一旦builder把数据构建到Map里面。这个map的数据将不能更改。

    使用builder.build方法构建Map

    package com.xuecheng.framework.exception;
    
    import com.google.common.collect.ImmutableMap;
    import com.xuecheng.framework.model.response.CommonCode;
    import com.xuecheng.framework.model.response.ResponseResult;
    import com.xuecheng.framework.model.response.ResultCode;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.converter.HttpMessageNotReadableException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @ControllerAdvice
    public class ExceptionCatch {
        private static final Logger LOGGER= LoggerFactory.getLogger(ExceptionCatch.class);
        //定义Map 配置异常类型所对应的错误代码
        private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;
        //定义map的builder对象,去构建ImmutableMap
        protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder=ImmutableMap.builder();
        //捕获CustomException此类异常
        @ExceptionHandler(CustomException.class)
        @ResponseBody
        public ResponseResult customException(CustomException customException){
            //获取异常信息,日志记录异常
            LOGGER.error("catch exception:{}",customException.getMessage());
            ResultCode resultCode=customException.getResultCode();
            return new ResponseResult(resultCode);
        }
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public ResponseResult exception(Exception exception){
            //记录日志
            LOGGER.error("catch exception:{}",exception.getMessage());
            if (EXCEPTIONS==null){
                builder.build();//EXCEPTIONS构建成功
            }
            //从EXCEPTIONS对象最后中获取大当前的这个传递过来的exception的错误代码,
            ResultCode resultCode=EXCEPTIONS.get(exception.getClass());
            if(resultCode!=null){
                return new ResponseResult(resultCode);//最终返回这个错误代码。
            }else{
                return new ResponseResult(CommonCode.SERVER_ERROR);
            }
        }
    
        static {
            //把异常的类和错误代码加入到builder对象里面。
            builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM);
        }
    }

    测试

    http://localhost:31001/cms/page/add

    在这里加一个断点

    空指针异常


    这里应该用EXCEPTIONS来接收build构建的map对象。

    if (EXCEPTIONS==null){
    EXCEPTIONS=builder.build();//EXCEPTIONS构建成功
    }


    再次测试

    找到了错误代码并返回。





    把这个请求改成get再去请求数据。返回的错误代码是 99999 因为这个错误没有加入到我们错误Map集合中。


    以上不可预知异常处理就做完了。

    完整代码

    package com.xuecheng.framework.exception;
    
    import com.google.common.collect.ImmutableMap;
    import com.xuecheng.framework.model.response.CommonCode;
    import com.xuecheng.framework.model.response.ResponseResult;
    import com.xuecheng.framework.model.response.ResultCode;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.converter.HttpMessageNotReadableException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @ControllerAdvice
    public class ExceptionCatch {
        private static final Logger LOGGER= LoggerFactory.getLogger(ExceptionCatch.class);
        //定义Map 配置异常类型所对应的错误代码
        private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;
        //定义map的builder对象,去构建ImmutableMap
        protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder=ImmutableMap.builder();
        //捕获CustomException此类异常
        @ExceptionHandler(CustomException.class)
        @ResponseBody
        public ResponseResult customException(CustomException customException){
            //获取异常信息,日志记录异常
            LOGGER.error("catch exception:{}",customException.getMessage());
            ResultCode resultCode=customException.getResultCode();
            return new ResponseResult(resultCode);
        }
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public ResponseResult exception(Exception exception){
            //记录日志
            LOGGER.error("catch exception:{}",exception.getMessage());
            if (EXCEPTIONS==null){
                EXCEPTIONS=builder.build();//EXCEPTIONS构建成功
            }
            //从EXCEPTIONS对象最后中获取大当前的这个传递过来的exception的错误代码,
            ResultCode resultCode=EXCEPTIONS.get(exception.getClass());
            if(resultCode!=null){
                return new ResponseResult(resultCode);//最终返回这个错误代码。
            }else{
                return new ResponseResult(CommonCode.SERVER_ERROR);
            }
        }
    
        static {
            //把异常的类和错误代码加入到builder对象里面。
            builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM);
        }
    }
    ExceptionCatch






     

  • 相关阅读:
    Git 常用命令整理
    用myeclipse打jar包,使其包含依赖jar包的指向
    解决 spring mvc3.1下post json出现HTTP Status 400 The request sent by the client was syntactically incorrect
    springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
    WIN8下git报错解决,fatal: Not a git repository (or any of the parent directories): .git
    Modernizr 用法
    SPRING MVC 的请求参数获取的几种方法
    spring mvc 缓存
    Spring+SpringMVC+Log4J
    Log4j使用总结
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11578663.html
Copyright © 2020-2023  润新知