美文网首页
异常处理映射问题解决

异常处理映射问题解决

作者: 弹钢琴的崽崽 | 来源:发表于2020-04-12 14:23 被阅读0次

1. 异常处理的问题分析

从添加页面的service方法中找问题:

//添加页面
public CmsPageResult add(CmsPage cmsPage){
    //校验页面是否存在,根据页面名称、站点Id、页面webpath查询        
    CmsPage cmsPage1 =cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),
                                       cmsPage.getSiteId(), cmsPage.getPageWebPath());
    if(cmsPage1==null){
        cmsPage.setPageId(null);//添加页面主键由spring data 自动生成
        cmsPageRepository.save(cmsPage);
        //返回结果
        CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage);
        return cmsPageResult;
    }
    return new CmsPageResult(CommonCode.FAIL,null);
}

问题:

  1. 上边的代码只要操作不成功仅向用户返回“错误代码:11111,失败信息:操作失败”,无法区别具体的错误信
    息。
  2. service方法在执行过程出现异常在哪捕获?在service中需要都加try/catch,如果在controller也需要添加
    try/catch,代码冗余严重且不易维护。

解决方案:

  1. 在Service方法中的编码顺序是先校验判断,有问题则抛出具体的异常信息,最后执行具体的业务操作,返回成
    功信息。
  2. 在统一异常处理类中去捕获异常,无需controller捕获异常,向用户返回统一规范的响应信息

代码模板如下:

//添加页面
public CmsPageResult add(CmsPage cmsPage){
    //校验cmsPage是否为空
    if(cmsPage == null){
        //抛出异常,非法请求
        //...
    }
    //根据页面名称查询(页面名称已在mongodb创建了唯一索引)
    CmsPage cmsPage1 =
        cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),
                                                     cmsPage.getSiteId(), cmsPage.getPageWebPath());
    //校验页面是否存在,已存在则抛出异常
    if(cmsPage1 !=null){
        //抛出异常,已存在相同的页面名称
        //...
    }
    cmsPage.setPageId(null);//添加页面主键由spring data 自动生成
    CmsPage save = cmsPageRepository.save(cmsPage);
    //返回结果
    CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,save);
    return cmsPageResult;
}

2. 异常处理流程

系统对异常的处理使用统一的异常处理流程:

  1. 自定义异常类型。
  2. 自定义错误代码及错误信息。
  3. 对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获。
    可预知异常是程序员在代码中手动抛出本系统定义的特定异常类型,由于是程序员抛出的异常,通常异常信息比较
    齐全,程序员在抛出时会指定错误代码及错误信息,获取异常信息也比较方便。
  4. 对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常。
    不可预知异常通常是由于系统出现bug、或一些不要抗拒的错误(比如网络中断、服务器宕机等),异常类型为
    RuntimeException类型(运行时异常)。
  5. 可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随
    请求响应给客户端。

异常抛出及处理流程:

  1. 在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型
  2. 统一由异常捕获类捕获异常,并进行处理
  3. 捕获到自定义异常则直接取出错误代码及错误信息,响应给用户。
  4. 捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误
    信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。
  5. 将错误代码及错误信息以Json格式响应给用户

3. 可预知异常处理

3.1 自定义异常类

package com.xuecheng.framework.exception;
public class CustomException extends RuntimeException {
    //错误代码
    ResultCode resultCode;

    public CustomException(ResultCode resultCode){
        this.resultCode = resultCode;
    }
    public ResultCode getResultCode(){
        return resultCode;
    }
}
package com.xuecheng.framework.model.response;
public interface ResultCode {
    //操作是否成功,true为成功,false操作失败
    boolean success();
    //操作代码
    int code();
    //提示信息
    String message();
}

3.2 异常抛出类

package com.xuecheng.framework.exception;
public class ExceptionCast {
    public static void cast(ResultCode resultCode){
        throw new CustomException(resultCode);
    }
}

3.3 异常捕获类

使用@ControllerAdvice@ExceptionHandler注解来捕获指定类型的异常

package com.xuecheng.framework.exception;
@ControllerAdvice//控制器增强
public class ExceptionCatch {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    //捕获CustomException此类异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult customException(CustomException customException){
        //记录日志
        LOGGER.error("catch exception:{}",customException.getMessage());
        ResultCode resultCode = customException.getResultCode();
        return new ResponseResult(resultCode);
    }
}

3.4 异常处理测试

3.4.1 定义错误代码

每个业务操作的异常使用异常代码去标识。

package com.xuecheng.framework.domain.cms.response; 
@ToString
public enum CmsCode implements ResultCode {
    CMS_ADDPAGE_EXISTS(false,24001,"页面已存在!");
    //操作结果
    boolean success; 
    //操作代码
    int code;
    //提示信息
    String message;
    private CmsCode(boolean success, int code, String message){
        this.success = success;
        this.code = code;
        this.message = message;
    }
    @Override
    public boolean success() {
        return success;
    }
    @Override
    public int code() {
        return code;
    }
    @Override
    public String message() {
        return message;
    }
}

3.4.2 业务中使用

页面存在时抛出异常

ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);

// 校验页面是否存在,根据页面名称、站点Id、页面webpath查询
    CmsPage cmsPage1 =
    cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),
                                                            cmsPage.getSiteId(), cmsPage.getPageWebPath());
if(cmsPage1 !=null){
    //校验页面是否存在,已存在则抛出异常
    ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);
}

3.4.3 启动工程,扫描到异常捕获的类ExceptionCatch

在springBoot的启动类中添加

 @ComponentScan(basePackages="com.xuecheng.framework")//扫描common工程下的类

4. 不可预知异常处理

当抛出不可预知异常时如何捕获呢?

如:不输入cmsPost信息请求服务时会抛出异常

org.springframework.http.converter.HttpMessageNotReadableException
//此异常是springMVC在进行参数转换时报的错误。

在异常捕获类中添加对Exception异常的捕获:

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

4.1 异常捕获方法

针对上边的问题其解决方案是:

  1. 我们在map中配置HttpMessageNotReadableException和错误代码。
  2. 在异常捕获类中对Exception异常进行捕获,并从map中获取异常类型对应的错误代码,如果存在错误代码则返
    回此错误,否则统一返回99999错误。

具体的开发实现如下:

  1. 在通用错误代码类CommCode中配置非法参数异常

    INVALID_PARAM(false,10003,"非法参数!"),
    
  2. 在异常捕获类中配置 HttpMessageNotReadableException 为非法参数异常。

    异常捕获类代码如下:

    package com.xuecheng.framework.exception;
    @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);
        }
        //捕获Exception此类异常
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public ResponseResult exception(Exception exception){
            //记录日志
            LOGGER.error("catch exception:{}",exception.getMessage());
            if(EXCEPTIONS == null){
                EXCEPTIONS = builder.build();//EXCEPTIONS构建成功
            }
            //从EXCEPTIONS中找异常类型所对应的错误代码,如果找到了将错误代码响应给用户,如果找不到给用户响应99999异常
            ResultCode resultCode = EXCEPTIONS.get(exception.getClass());
            if(resultCode !=null){
                return new ResponseResult(resultCode);
            }else{
                //返回99999异常
                return new ResponseResult(CommonCode.SERVER_ERROR);
            }
    
    
        }
    
        static {
            //定义异常类型所对应的错误代码
            builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM);
        }
    }
    

使用ImmutableMap集合。一个不可变集合

相关文章

网友评论

      本文标题:异常处理映射问题解决

      本文链接:https://www.haomeiwen.com/subject/mngvmhtx.html