学成在线 第3天 讲义-CMS页面管理开发八
程序员文章站
2022-05-30 21:35:23
...
5.3 可预知异常处理
5.3.1 自定义异常类
在common工程定义异常类型。
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 异常抛出类
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注解来捕获指定类型的异常
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定义错误代码
每个业务操作的异常使用异常代码去标识。
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、抛出异常
在controller、service、 dao中都可以抛出异常。
修改PageService的add方法,添加抛出异常的代码
/校验页面是否存在,根据页面名称、站点Id、页面webpath查询
CmsPage cmsPage1 =
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath(cmsPage.
getPageName(),
cmsPage.
getSiteId(), cmsPage.
getPageWebPath());
if(cmsPage1 !
=null){
//校验页面是否存在,已存在则抛出异常
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);
}
2、启动工程,扫描到异常捕获的类ExceptionCatch
在springBoot的启动类中添加
@ComponentScan(basePackages
=
"
com.xuecheng
.framework
"
)//扫描common工程下的类
3、前端展示异常信息
服务端响应信息如下:
页面提取异常处理
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(
'
提交失败
'
);
}
});
});
}
});
}
下一篇: PHP 5克隆函数应用浅析_PHP教程