A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】学成在线 第3天 讲义-CMS页面管理开发八

5.3 可预知异常处理
5.3.1 自定义异常类
common工程定义异常类型。
[AppleScript] 纯文本查看 复制代码
 package com.xuecheng
.framework.exception;
import com.xuecheng
.framework.model.response.ResultCode;
public class CustomException extends RuntimeException {
private ResultCode resultCode;
public CustomException(ResultCode resultCode) {
//异常信息为错误代码+异常信息
super(
"
错误代码:
"
+resultCode.code()+
"
错误信息:
"
+resultCode.message());
this.resultCode
= resultCode;
}
public ResultCode getResultCode(){
return this.resultCode;
}
}

5.3.2 异常抛出类
[AppleScript] 纯文本查看 复制代码
 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);
}
}


5.3.3 异常捕获类
使用 @ControllerAdvice@ExceptionHandler注解来捕获指定类型的异常
[AppleScript] 纯文本查看 复制代码
 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;
}
}


5.3.4异常处理测试
5.3.4.1定义错误代码
每个业务操作的异常使用异常代码去标识。
[AppleScript] 纯文本查看 复制代码
 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_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;
}
} 


5.3.4.2 异常处理测试
1、抛出异常
controllerservicedao中都可以抛出异常。
修改PageServiceadd方法,添加抛出异常的代码
[AppleScript] 纯文本查看 复制代码
 /校验页面是否存在,根据页面名称、站点Id、页面webpath查询
CmsPage cmsPage1 =
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath(cmsPage.
getPageName(),
cmsPage.
getSiteId(), cmsPage.
getPageWebPath());
if(cmsPage1 !
=null){
//校验页面是否存在,已存在则抛出异常
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);
} 


2、启动工程,扫描到异常捕获的类ExceptionCatch
[AppleScript] 纯文本查看 复制代码
在springBoot的启动类中添加
@ComponentScan(basePackages
=
"
com.xuecheng
.framework
"
)//扫描common工程下的类


3、前端展示异常信息
服务端响应信息如下:


页面提取异常处理
[AppleScript] 纯文本查看 复制代码
 addSubmit(){
this.
$refs.
pageForm.validate((valid)
=
> {
if (valid) {
this.
$confirm(
'
确认提交吗?
'
,
'
提示
'
, {})
.then(()
=
> {
cmsApi.
page_add(this.
pageForm)
.then((res)
=
> {
console.log(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.error(
'
提交失败
'
);
}
});
});
}
});
}



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马