学成在线项目学习之自定义异常

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



package com.xuecheng.framework.exception;

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

/**我们自定义异常类,我们所能预见到的异常 都被封装在resultCode中
* 该类实现两个功能,定义我们自定义异常的异常信息,而是必须包含resultCode属性
* 我们返给前端 ResponseResult该类的对象,该类的属性来自于resultCode.
*/
public class CustomException extends RuntimeException {
public ResultCode getResultCode() {
return resultCode;
}
private ResultCode resultCode;

public CustomException(ResultCode resultCode) {
//异常信息为错误代码+异常信息(自定义我们的异常信息格式)
super("错误代码:" + resultCode.code() + "错误信息:" + resultCode.message());
this.resultCode = resultCode;
}
}

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



@Service
public class PageService {

@Autowired
CmsPageRepository cmsPageRepository;

//添加页面
public CmsPageResult add(CmsPage cmsPage) {
//校验页面是否存在,根据页面名称,站点Id,页面webPath查询(是否需要判断cmsPage是否为null呢)
if (cmsPage == null) {
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_DATAISNULL);
cmsPage = new CmsPage();//对象的成员属性都有初始化值
}
CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(
cmsPage.getPageName(),
cmsPage.getSiteId(),
cmsPage.getPageWebPath());

if (cmsPage1 != null) {
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
}

cmsPage.setPageId(null);//添加页面主键id为null,让主键由spring data mongodb自动为我们生成
// CmsPage cmspage = cmsPageRepository.save(cmsPage);//返回插入数据库后的对象
cmsPageRepository.save(cmsPage);
//返回结果
CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS, cmsPage);
return cmsPageResult;
}

}

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;


@ControllerAdvice
public class ExceptionCatch {
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
//捕获 CustomException异常
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseResult customException(CustomException e) {
LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e);//打印被抓取到的自定义的异常类信息到控制台
ResultCode resultCode = e.getResultCode();
ResponseResult responseResult = new ResponseResult(resultCode);
return responseResult;
}
}



 
 
posted @ 2018-12-06 14:16  1314520jinxuexia  阅读(948)  评论(0编辑  收藏  举报