Open wangchengming666 opened 4 days ago
在使用sofa-hessian-go这个库时,使用hessian序列化的场景下,用户手动构造com.alipay.sofa.rpc.core.response.SofaResponse会导致异常。
com.alipay.sofa.rpc.core.response.SofaResponse
SofaRPCResponse的结构体如下
type SofaRPCResponse struct { IsError bool `hessian:"isError"` ErrorMsg string `hessian:"errorMsg"` AppResponse interface{} `hessian:"appResponse"` ResponseProps map[string]string `hessian:"responseProps"` } func (s *SofaRPCResponse) GetJavaClassName() string { return "com.alipay.sofa.rpc.core.response.SofaResponse" }
可以看到errorMsg为string类型,在go里string会被初始化为空字符串。此时用户想自定义appResponse这个属性为一个xxxRuntimeException,并没有设置errorMsg这个属性。
appResponse
xxxRuntimeException
errorMsg
那么在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里,仅仅判断了errorMsg为null的场景并没有判断为空字符串的场景,那么就会将isError设置为true,导致异常类型为SofaRpcException而不是用户手动设置的xxxRuntimeException。
com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg
errorMsg为null
没有判断为空字符串
isError设置为true
SofaRpcException
解决方案: 可以在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里使用StringUtils.isEmpty进行判断
在使用sofa-hessian-go这个库时,使用hessian序列化的场景下,用户手动构造
com.alipay.sofa.rpc.core.response.SofaResponse
会导致异常。SofaRPCResponse的结构体如下
可以看到errorMsg为string类型,在go里string会被初始化为空字符串。此时用户想自定义
appResponse
这个属性为一个xxxRuntimeException
,并没有设置errorMsg
这个属性。那么在
com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg
里,仅仅判断了errorMsg为null
的场景并没有判断为空字符串
的场景,那么就会将isError设置为true
,导致异常类型为SofaRpcException
而不是用户手动设置的xxxRuntimeException
。解决方案: 可以在
com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg
里使用StringUtils.isEmpty进行判断