cms-服务端统一异常处理

异常处理

异常处理的问题分析

从添加页面的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;
}	

异常处理流程

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

1、自定义异常类型。

2、自定义错误代码及错误信息。

3、对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获。

可预知异常是程序员在代码中手动抛出本系统定义的特定异常类型,由于是程序员抛出的异常,通常异常信息比较

齐全,程序员在抛出时会指定错误代码及错误信息,获取异常信息也比较方便。

4、对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常。

不可预知异常通常是由于系统出现bug、或一些不要抗拒的错误(比如网络中断、服务器宕机等),异常类型为

RuntimeException类型(运行时异常)。

5、可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随

请求响应给客户端。

异常抛出及处理流程:

1564670785760

1、在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型

2、统一由异常捕获类捕获异常,并进行处理

3、捕获到自定义异常则直接取出错误代码及错误信息,响应给用户。

4、捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。

5、将错误代码及错误信息以Json格式响应给用户。

可预知异常处理

自定义异常类

在common工程定义异常类型。

package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
/**
 * 自定义异常类
 * @author Hr|黄锐
 * @create 2019/8/1 20:30
 */
//继承运行时异常,好处是对代码没有侵入性,比如: 定义普通异常的话,会报错,必须捕获异常才行
public class CustomException extends RuntimeException {

    //错误代码
    private ResultCode resultCode;

    public CustomException(ResultCode resultCode) {
        this.resultCode = resultCode;
    }
    //取出错误代码
    public ResultCode getResultCode() {
        return resultCode;
    }
}

此类作用是继承RuntimeException异常,ResultCode作为构造参数传进去,并定义获取错误代码方法

异常抛出类

package com.xuecheng.framework.exception;

import com.xuecheng.framework.model.response.ResultCode;

/**
 * 异常抛出类
 * @author Hr|黄锐
 * @create 2019/8/1 20:41
 */
public class ExceptionCast {
    public static void cast(ResultCode resultCode) {
        throw new CustomException(resultCode);
    }
}

此类作用是,将上面自定义异常类进行封装,通过静态方法直接调用传参,不用自己new

异常捕获类

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

package com.xuecheng.framework.exception;

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.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一的异常捕获类,用来捕获自定的所有异常,避免代码中重复捕获,代码冗余
 * @author Hr|黄锐
 * @create 2019/8/1 20:44
 */

@ControllerAdvice//控制器增强
public class ExceptionCatch {
//把出错的信息进行日志记录
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    //捕获CustomException类异常
    @ExceptionHandler(CustomException.class)//指定捕获哪一类异常
    @ResponseBody//以后要将返回的ResponseResult对象数据转为json类型返回
    public ResponseResult customException(CustomException customException) {
//        记录日志
        LOGGER.error("catch exception:{}", customException.getMessage());
//        拿到抛出的异常对象,获取异常信息
        ResultCode resultCode = customException.getResultCode();
        return new ResponseResult(resultCode);
    }

}

此类作用是,对抛出的自定义异常进行捕获,调用获取异常信息方法,获取错误信息,并转为统一的ResponseResult返回给前端,由于前段显示的错误信息数据类型为json,所以使用@ResponseBody注解转为json,并再捕获时进行日志记录.

异常处理测试

定义错误代码

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

在cms工程中,有对应的cms处理的异常代码处理类,cmsCode类

package com.xuecheng.framework.domain.cms.response;

import com.xuecheng.framework.model.response.ResultCode;
import lombok.ToString;

/**
 * Created by mrt on 2018/3/5.
 */
@ToString
public enum CmsCode implements ResultCode {
    CMS_ADDPAGE_EXISTSNAME(false,24001,"页面名称已存在!"),
    CMS_GENERATEHTML_DATAURLISNULL(false,24002,"从页面信息中找不到获取数据的url!"),
    CMS_GENERATEHTML_DATAISNULL(false,24003,"根据页面的数据url获取不到数据!"),
    CMS_GENERATEHTML_TEMPLATEISNULL(false,24004,"页面模板为空!"),
    CMS_GENERATEHTML_HTMLISNULL(false,24005,"生成的静态html为空!"),
    CMS_GENERATEHTML_SAVEHTMLERROR(false,24005,"保存静态html出错!"),
    CMS_COURSE_PERVIEWISNULL(false,24007,"预览页面为空!"),
    CMS_ERROR_PARAMETER(false, 24008, "非法参数异常!");
    //操作代码
    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;
    }
}

异常处理测试

1、抛出异常

在controller、service、 dao中都可以抛出异常。

修改PageService的add方法,添加抛出异常的代码

 if (cmsPage == null) {
//            抛出异常,非法参数异常,cmsPage参数为空
            ExceptionCast.cast(CmsCode.CMS_ERROR_PARAMETER);
        }
        //校验页面名称,webPath,页面名称唯一性
        //根据上述去页面查询集合,查到就说明已存在,查询不到就可以添加
        CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
        if (cmsPage1 != null) {
            //页面已经存在
            //抛出页面已存在异常
            ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
        }

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

在springBoot的启动类中添加

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

前端页面提取异常处理

 addSubmit(){
        this.$refs['pageForm'].validate((valid) => {
          if (valid) {
            //加一个确认提示,参见element-ui
            this.$confirm('您确认提交吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              //调用服务端请求新增页面接口
              cmsApi.page_add(this.pageForm).then(res=>{
                //解析响应内容
                if (res.success){
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  })
                  //清空表单
                  this.$refs['pageForm'].resetFields();
                }else if (res.message){
                  this.$message.error(res.message);
                }else {
                  /*this.$message({
                    message: '提交失败',
                    type: 'error'
                  })*/
                  //等效下面
                  this.$message.error("提交失败");
                }
              })
            })
          } else {
            console.log('校验失败');
            return false;
          }
        });
      },

增加一层else if判断,如果响应的json信息中有message这一项数据,就直接在前端页面提示显示客户端异常信息内容

不可预知异常处理

定义异常捕获方法

使用postman测试添加页面,不输入cmsPost信息,提交,报错信息如下:

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

具体的响应的信息为:

{
"timestamp": 1528712906727,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public
com.xuecheng.framework.domain.cms.response.CmsPageResult
com.xuecheng.manage_cms.web.controller.CmsPageController.add(com.xuecheng.framework.domain.cms.C
msPage)",
"path": "/cms/page/add"
}

上边的响应信息在客户端是无法解析的。

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

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

异常捕获方法

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

1、我们在map中配置HttpMessageNotReadableException和错误代码。

2、在异常捕获类中对Exception异常进行捕获,并从map中获取异常类型对应的错误代码,如果存在错误代码则返

回此错误,否则统一返回99999错误。

具体的开发实现如下:

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

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

2、在异常捕获类中配置 HttpMessageNotReadableException 为非法参数异常。 异常捕获类代码如下:

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;

/**
 * 统一的异常捕获类,用来捕获自定的所有异常,避免代码中重复捕获,代码冗余
 * @author Hr|黄锐
 * @create 2019/8/1 20:44
 */

@ControllerAdvice//控制器增强
public class ExceptionCatch {
//把出错的信息进行日志记录
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    //    定义一个map,配置异常类型对应的错误代码
    /*ImmutableMap是google的一个工具包map类型,这个map类型的数据一旦放进去是不可修改的
    也就是只读的,并且它是线程安全的
    */
    private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
    //定义map的builder对象,去构建ImmutableMap
    protected static ImmutableMap.Builder<Class<? extends Throwable>, ResultCode> builder = ImmutableMap.builder();


    //捕获Exception类异常
    @ExceptionHandler(Exception.class)//指定捕获哪一类异常
    @ResponseBody//以后要将返回的ResponseResult对象数据转为json类型返回
    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);
    }
}


异常处理测试

仍然模拟“问题测试”中的测试步骤,异常结果为“非法参数”。

不可预知异常总结:

  1. ​ 定义ImmutableMap 的map类型,定义它的builder对象
  2. 再类中添加static静态代码块,罗列出可处理的异常类型,builder.put方法传入异常类型,和错误代码
  3. 在Exception异常处理方法中判断有没有ImmutableMap 这个对象,没有就构建
  4. 从map中取出抛出的异常代码,如果不为空说明在map里,就返回map里的异常信息,否则返回99999

HuangRui

Every man dies, not every man really lives.

HaungRui, China suixinblog.cn