学成在线--7.CMS页面管理开发(异常处理)

文章目录

  • 1.异常处理的问题分析
  • 2.异常处理流程
  • 3.可预知异常处理
    • 1.自定义异常类
    • 2.异常抛出类
    • 3.异常捕获类
    • 4.异常处理测试
      • 1)定义错误代码
      • 2)异常处理测试
  • 4.不可预知异常处理
    • 1.定义异常捕获方法
      • 1)异常抛出测试
      • 2)异常捕获方法

1.异常处理的问题分析

从添加页面的service方法中找问题:
文件位置:com/ xuecheng/ manage_cms/ service/ PageService

//添加页面
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、可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随请求响应给客户端。

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

3.可预知异常处理

1.自定义异常类

文件位置:com/ xuecheng/ framework/ exception/ CustomException

package com.xuecheng.framework.exception;

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

/**
 * 自定义异常类型
 **/
public class CustomException extends RuntimeException {
     

    //错误代码
    ResultCode resultCode;

    public CustomException(ResultCode resultCode){
     
        this.resultCode = resultCode;
    }
    public ResultCode getResultCode(){
     
        return resultCode;
    }
    
}

2.异常抛出类

文件位置:com/ xuecheng/ framework/ exception/ ExceptionCast

package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
public class ExceptionCast {
     
//使用此静态方法抛出自定义异常
public static void cast(ResultCode resultCode){
     
throw new CustomException(resultCode);
}
}

3.异常捕获类

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

文件位置:com/ xuecheng/ framework/ exception/ ExceptionCatch

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


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

4.异常处理测试

1)定义错误代码

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

文件位置:com/ xuecheng/ framework/ domain/ cms/ response / CmsCode

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

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

@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,"预览页面为空!");
    //操作代码
    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;
    }
}

其中继承的接口ResultCode如下:

package com.xuecheng.framework.model.response;

/**
 * Created by mrt on 2018/3/5.
 * 10000-- 通用错误代码
 * 22000-- 媒资错误代码
 * 23000-- 用户中心错误代码
 * 24000-- cms错误代码
 * 25000-- 文件系统
 */
public interface ResultCode {
     
    //操作是否成功,true为成功,false操作失败
    boolean success();
    //操作代码
    int code();
    //提示信息
    String message();

}

2)异常处理测试

1.抛出异常
在controller、service、 dao中都可以抛出异常。
修改PageService的add方法,添加抛出异常的代码

文件位置:com/ xuecheng/ manage_cms/ service/ PageService

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

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

文件位置:com/ xuecheng/ manage_cms/ ManageCmsApplication

在springBoot的启动类中添加

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

3、前端展示异常信息

服务端响应信息如下:
学成在线--7.CMS页面管理开发(异常处理)_第2张图片
前端页面提取异常处理
文件位置:src/ module/ cms/ page/ page_add.vue

      addSubmit:function(){
     
        this.$refs['pageForm'].validate((valid) => {
     
          if (valid) {
     //表单校验成功
            //确认提示
            this.$confirm('您确认提交吗?', '提示', {
      }).then(() => {
     
              //调用page_add方法请求服务端的新增页面接口
              cmsApi.page_add(this.pageForm).then(res=>{
     
                //解析服务端响应内容
                if(res.success){
     
                  /*this.$message({
                    message: '提交成功',
                    type: 'success'
                  })*/
                  this.$message.success("提交成功")
                  //将表单清空
                  this.$refs['pageForm'].resetFields();
                }else if(res.message){
     
                  this.$message.error(res.message)
                }else{
     
                  this.$message.error("提交失败")
                }
              });
            })

          }
        });
      },

4.不可预知异常处理

1.定义异常捕获方法

1)异常抛出测试

使用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"
}

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

2)异常捕获方法

针对上边的问题其解决方案是:
1、我们在map中配置HttpMessageNotReadableException和错误代码。
2、在异常捕获类中对Exception异常进行捕获,并从map中获取异常类型对应的错误代码,如果存在错误代码则返
回此错误,否则统一返回99999错误。
具体的开发实现如下:
1、在通用错误代码类CommCode中配置非法参数异常
文件位置:com/xuecheng/framework/model/response/CommonCode

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

2.在异常捕获类中添加对Exception异常的捕获,并配置 HttpMessageNotReadableException 为非法参数异常

文件位置:com/ xuecheng/ framework/ exception/ ExceptionCatch

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 Administrator
 * @version 1.0
 * @create 2018-09-14 17:32
 **/
@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);
    }
}

你可能感兴趣的:(Java--学成在线)