|
@@ -8,10 +8,15 @@ import (
|
|
|
|
|
|
|
|
// CommonError 微信返回的通用错误json
|
|
// CommonError 微信返回的通用错误json
|
|
|
type CommonError struct {
|
|
type CommonError struct {
|
|
|
|
|
+ apiName string
|
|
|
ErrCode int64 `json:"errcode"`
|
|
ErrCode int64 `json:"errcode"`
|
|
|
ErrMsg string `json:"errmsg"`
|
|
ErrMsg string `json:"errmsg"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (c *CommonError) Error() string {
|
|
|
|
|
+ return fmt.Sprintf("%s Error , errcode=%d , errmsg=%s", c.apiName, c.ErrCode, c.ErrMsg)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// DecodeWithCommonError 将返回值按照CommonError解析
|
|
// DecodeWithCommonError 将返回值按照CommonError解析
|
|
|
func DecodeWithCommonError(response []byte, apiName string) (err error) {
|
|
func DecodeWithCommonError(response []byte, apiName string) (err error) {
|
|
|
var commError CommonError
|
|
var commError CommonError
|
|
@@ -19,8 +24,9 @@ func DecodeWithCommonError(response []byte, apiName string) (err error) {
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+ commError.apiName = apiName
|
|
|
if commError.ErrCode != 0 {
|
|
if commError.ErrCode != 0 {
|
|
|
- return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg)
|
|
|
|
|
|
|
+ return &commError
|
|
|
}
|
|
}
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
@@ -45,7 +51,11 @@ func DecodeWithError(response []byte, obj interface{}, apiName string) error {
|
|
|
return fmt.Errorf("errcode or errmsg is invalid")
|
|
return fmt.Errorf("errcode or errmsg is invalid")
|
|
|
}
|
|
}
|
|
|
if errCode.Int() != 0 {
|
|
if errCode.Int() != 0 {
|
|
|
- return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, errCode.Int(), errMsg.String())
|
|
|
|
|
|
|
+ return &CommonError{
|
|
|
|
|
+ apiName: apiName,
|
|
|
|
|
+ ErrCode: errCode.Int(),
|
|
|
|
|
+ ErrMsg: errMsg.String(),
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|