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;
}
}
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);
}
}
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;
}
}
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;
}
}
/校验页面是否存在,根据页面名称、站点Id、页面webpath查询
CmsPage cmsPage1 =
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath(cmsPage.
getPageName(),
cmsPage.
getSiteId(), cmsPage.
getPageWebPath());
if(cmsPage1 !
=null){
//校验页面是否存在,已存在则抛出异常
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);
}
在springBoot的启动类中添加
@ComponentScan(basePackages
=
"
com.xuecheng
.framework
"
)//扫描common工程下的类
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(
'
提交失败
'
);
}
});
});
}
});
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |