From f2e7979a9fbdad61ce9a1e7632444623b6e052d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?s=C5=AB=20h=C7=8Ei?= Date: Tue, 17 May 2022 10:33:53 +0800 Subject: [PATCH] return *CommonError instead of errorString (#568) --- util/error.go | 14 ++++++++++-- util/error_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 util/error_test.go diff --git a/util/error.go b/util/error.go index b971c47..f9c835a 100644 --- a/util/error.go +++ b/util/error.go @@ -8,10 +8,15 @@ import ( // CommonError 微信返回的通用错误json type CommonError struct { + apiName string ErrCode int64 `json:"errcode"` 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解析 func DecodeWithCommonError(response []byte, apiName string) (err error) { var commError CommonError @@ -19,8 +24,9 @@ func DecodeWithCommonError(response []byte, apiName string) (err error) { if err != nil { return } + commError.apiName = apiName if commError.ErrCode != 0 { - return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg) + return &commError } return nil } @@ -45,7 +51,11 @@ func DecodeWithError(response []byte, obj interface{}, apiName string) error { return fmt.Errorf("errcode or errmsg is invalid") } 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 } diff --git a/util/error_test.go b/util/error_test.go new file mode 100644 index 0000000..50fc996 --- /dev/null +++ b/util/error_test.go @@ -0,0 +1,55 @@ +package util + +import "testing" + +var okErrData string = `{"errcode": 0}` +var errData string = `{"errcode": 43101, "errmsg": "user refuse to accept the msg"}` +var expectError string = "Send Error , errcode=43101 , errmsg=user refuse to accept the msg" + +func TestDecodeWithCommonErrorNoError(t *testing.T) { + err := DecodeWithCommonError([]byte(okErrData), "Send") + if err != nil { + t.Error("DecodeWithCommonError should not return error") + return + } +} + +func TestDecodeWithCommonError(t *testing.T) { + err := DecodeWithCommonError([]byte(errData), "Send") + if err == nil { + t.Error("DecodeWithCommonError should return error") + return + } + + cErr, ok := err.(*CommonError) + if !ok { + t.Errorf("DecodeWithCommonError should return *CommonError but %T", err) + return + } + if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) { + t.Error("DecodeWithCommonError return bad *CommonError") + return + } +} + +func TestDecodeWithError(t *testing.T) { + type DE struct { + CommonError + } + var obj DE + err := DecodeWithError([]byte(errData), &obj, "Send") + if err == nil { + t.Error("DecodeWithError should return error") + return + } + + cErr, ok := err.(*CommonError) + if !ok { + t.Errorf("DecodeWithError should return *CommonError but %T", err) + return + } + if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) { + t.Error("DecodeWithError return bad *CommonError") + return + } +}