Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
e04c9584
提交
e04c9584
authored
7月 18, 2020
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改 Dubbo Exception Filter 的处理,针对系统异常,还是直接抛出
上级
c0407267
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
21 行增加
和
20 行删除
+21
-20
DubboProviderExceptionFilter.java
.../mall/dubbo/core/filter/DubboProviderExceptionFilter.java
+12
-17
GlobalExceptionHandler.java
...iocoder/mall/web/core/handler/GlobalExceptionHandler.java
+9
-3
没有找到文件。
common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java
浏览文件 @
e04c9584
...
@@ -15,7 +15,8 @@ import javax.validation.ConstraintViolation;
...
@@ -15,7 +15,8 @@ import javax.validation.ConstraintViolation;
import
javax.validation.ConstraintViolationException
;
import
javax.validation.ConstraintViolationException
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.Type
;
import
static
cn
.
iocoder
.
common
.
framework
.
exception
.
enums
.
GlobalErrorCodeConstants
.*;
import
static
cn
.
iocoder
.
common
.
framework
.
exception
.
enums
.
GlobalErrorCodeConstants
.
BAD_REQUEST
;
import
static
cn
.
iocoder
.
common
.
framework
.
exception
.
enums
.
GlobalErrorCodeConstants
.
INTERNAL_SERVER_ERROR
;
@Activate
(
group
=
CommonConstants
.
PROVIDER
)
@Activate
(
group
=
CommonConstants
.
PROVIDER
)
public
class
DubboProviderExceptionFilter
implements
Filter
,
Filter
.
Listener
{
public
class
DubboProviderExceptionFilter
implements
Filter
,
Filter
.
Listener
{
...
@@ -31,30 +32,24 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
...
@@ -31,30 +32,24 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
public
void
onResponse
(
Result
appResponse
,
Invoker
<?>
invoker
,
Invocation
invocation
)
{
public
void
onResponse
(
Result
appResponse
,
Invoker
<?>
invoker
,
Invocation
invocation
)
{
if
(
appResponse
.
hasException
()
&&
GenericService
.
class
!=
invoker
.
getInterface
())
{
if
(
appResponse
.
hasException
()
&&
GenericService
.
class
!=
invoker
.
getInterface
())
{
try
{
try
{
// 转换异常
//
1.
转换异常
Throwable
exception
=
appResponse
.
getException
();
Throwable
exception
=
appResponse
.
getException
();
// 1. 参数校验异常
// 1.
1
参数校验异常
if
(
exception
instanceof
ConstraintViolationException
)
{
if
(
exception
instanceof
ConstraintViolationException
)
{
exception
=
this
.
constraintViolationExceptionHandler
((
ConstraintViolationException
)
exception
);
exception
=
this
.
constraintViolationExceptionHandler
((
ConstraintViolationException
)
exception
);
//
2
. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
//
1
. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
}
else
if
(
exception
instanceof
ServiceException
)
{
}
else
if
(
exception
instanceof
ServiceException
)
{
//
3.
其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
//
1.3
其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
}
else
{
}
else
{
exception
=
this
.
defaultExceptionHandler
(
exception
,
invocation
);
exception
=
this
.
defaultExceptionHandler
(
exception
,
invocation
);
assert
exception
!=
null
;
assert
exception
!=
null
;
}
}
// 根据不同的方法 schema 返回结果
// 2. 根据不同的方法 schema 返回结果
// 第一种情况,返回参数类型是 CommonResult 的情况,则将 ServiceException 转换成 CommonResult
// 2.1 如果是 ServiceException 异常,并且返回参数类型是 CommonResult 的情况,则将转换成 CommonResult 返回
if
(
isReturnCommonResult
(
invocation
))
{
if
(
isReturnCommonResult
(
invocation
)
&&
exception
instanceof
ServiceException
)
{
// 清空异常
appResponse
.
setException
(
null
);
// 一定要清空异常
appResponse
.
setException
(
null
);
appResponse
.
setValue
(
CommonResult
.
error
((
ServiceException
)
exception
));
// 设置结果
// 2.2 如果是 GlobalException 全局异常,则直接抛出
if
(
exception
instanceof
ServiceException
)
{
appResponse
.
setValue
(
CommonResult
.
error
((
ServiceException
)
exception
));
}
else
{
appResponse
.
setValue
(
CommonResult
.
error
((
GlobalException
)
exception
));
}
// 第二种情况,未包装成 CommonResult 的情况,则直接抛出 ServiceException 异常
}
else
{
}
else
{
appResponse
.
setException
(
exception
);
appResponse
.
setException
(
exception
);
}
}
...
...
common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java
浏览文件 @
e04c9584
...
@@ -160,9 +160,15 @@ public class GlobalExceptionHandler {
...
@@ -160,9 +160,15 @@ public class GlobalExceptionHandler {
*/
*/
@ExceptionHandler
(
value
=
GlobalException
.
class
)
@ExceptionHandler
(
value
=
GlobalException
.
class
)
public
CommonResult
globalExceptionHandler
(
HttpServletRequest
req
,
GlobalException
ex
)
{
public
CommonResult
globalExceptionHandler
(
HttpServletRequest
req
,
GlobalException
ex
)
{
logger
.
error
(
"[globalExceptionHandler]"
,
ex
);
// 系统异常时,才打印异常日志
// 插入异常日志
if
(
INTERNAL_SERVER_ERROR
.
getCode
().
equals
(
ex
.
getCode
()))
{
this
.
createExceptionLog
(
req
,
ex
);
logger
.
error
(
"[globalExceptionHandler]"
,
ex
);
// 插入异常日志
this
.
createExceptionLog
(
req
,
ex
);
// 普通全局异常,打印 info 日志即可
}
else
{
logger
.
info
(
"[globalExceptionHandler]"
,
ex
);
}
// 返回 ERROR CommonResult
// 返回 ERROR CommonResult
return
CommonResult
.
error
(
ex
);
return
CommonResult
.
error
(
ex
);
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论