sofa-rpc icon indicating copy to clipboard operation
sofa-rpc copied to clipboard

用户手动构造SofaResponse时并且errorMsg为空字符串场景会导致异常

Open wangchengming666 opened this issue 2 months ago • 0 comments

在使用sofa-hessian-go这个库时,使用hessian序列化的场景下,用户手动构造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这个属性。

那么在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里,仅仅判断了errorMsg为null的场景并没有判断为空字符串的场景,那么就会将isError设置为true,导致异常类型为SofaRpcException而不是用户手动设置的xxxRuntimeException

解决方案: 可以在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里使用StringUtils.isEmpty进行判断

wangchengming666 avatar Jun 27 '24 02:06 wangchengming666