Skip to content

Commit 8814bbd

Browse files
committed
polish:优化全局错误处理
1 parent 870c4ae commit 8814bbd

File tree

10 files changed

+73
-45
lines changed

10 files changed

+73
-45
lines changed

account.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -40,7 +39,7 @@ func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, e
4039
}
4140
_ = json.Unmarshal(data, &info)
4241
if info.ErrCode != 0 {
43-
return info, errors.New(info.ErrMsg)
42+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
4443
}
4544
return info, nil
4645
}
@@ -58,7 +57,7 @@ func (r *Client) AccountDel(options AccountDelOptions) (info BaseModel, err erro
5857
}
5958
_ = json.Unmarshal(data, &info)
6059
if info.ErrCode != 0 {
61-
return info, errors.New(info.ErrMsg)
60+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
6261
}
6362
return info, nil
6463
}
@@ -78,7 +77,7 @@ func (r *Client) AccountUpdate(options AccountUpdateOptions) (info BaseModel, er
7877
}
7978
_ = json.Unmarshal(data, &info)
8079
if info.ErrCode != 0 {
81-
return info, errors.New(info.ErrMsg)
80+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
8281
}
8382
return info, nil
8483
}
@@ -104,7 +103,7 @@ func (r *Client) AccountList() (info AccountListSchema, err error) {
104103
}
105104
_ = json.Unmarshal(data, &info)
106105
if info.ErrCode != 0 {
107-
return info, errors.New(info.ErrMsg)
106+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
108107
}
109108
return info, nil
110109
}
@@ -129,7 +128,7 @@ func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWay
129128
}
130129
_ = json.Unmarshal(data, &info)
131130
if info.ErrCode != 0 {
132-
return info, errors.New(info.ErrMsg)
131+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
133132
}
134133
return info, nil
135134
}

client.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,8 @@ func New(options Options) (client *Client, err error) {
5757
mutex: sync.Mutex{},
5858
}
5959

60-
//判断是否已初始化完成,如果己初始化则直接返回当前实例
61-
token, err := client.getAccessToken()
62-
if err != nil {
63-
return nil, errors.New("cache unavailable")
64-
}
65-
66-
if token == "" {
67-
//初始化AccessToken
68-
tokenInfo, err := client.GetAccessToken()
69-
if err != nil {
70-
return nil, err
71-
}
72-
73-
if err = client.setAccessToken(tokenInfo.AccessToken); err != nil {
74-
return nil, err
75-
}
76-
client.accessToken = tokenInfo.AccessToken
77-
} else {
78-
client.accessToken = token
60+
if err = client.initAccessToken(); err != nil {
61+
return nil, err
7962
}
8063

8164
return client, nil

customer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -40,7 +39,7 @@ func (r *Client) CustomerBatchGet(options CustomerBatchGetOptions) (info Custome
4039
}
4140
_ = json.Unmarshal(data, &info)
4241
if info.ErrCode != 0 {
43-
return info, errors.New(info.ErrMsg)
42+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
4443
}
4544
return info, nil
4645
}

error.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package WeChatCustomerServiceSDK
2+
3+
import "fmt"
4+
5+
// Error 错误
6+
type Error struct {
7+
ErrCode int `json:"err_code,omitempty"`
8+
ErrMsg string `json:"err_msg"`
9+
}
10+
11+
//输出错误信息
12+
func (r Error) Error() string {
13+
return fmt.Sprintf("%d:%s", r.ErrCode, r.ErrMsg)
14+
}
15+
16+
// NewSDKErr 初始化SDK实例错误信息
17+
func NewSDKErr(code int, msg string) Error {
18+
return Error{
19+
ErrCode: code,
20+
ErrMsg: msg,
21+
}
22+
}

media.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
"mime/multipart"
@@ -55,7 +54,7 @@ func (r *Client) MediaUpload(options MediaUploadOptions) (info MediaUploadSchema
5554
_ = json.Unmarshal(data, &info)
5655
fmt.Println(string(data))
5756
if info.ErrCode != 0 {
58-
return info, errors.New(info.ErrMsg)
57+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
5958
}
6059
return info, nil
6160
}

sendmsg.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -26,7 +25,7 @@ func (r *Client) SendMsg(options interface{}) (info SendMsgSchema, err error) {
2625
}
2726
_ = json.Unmarshal(data, &info)
2827
if info.ErrCode != 0 {
29-
return info, errors.New(info.ErrMsg)
28+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
3029
}
3130
return info, nil
3231
}

servicer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -39,7 +38,7 @@ func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info Receptionist
3938
}
4039
_ = json.Unmarshal(data, &info)
4140
if info.ErrCode != 0 {
42-
return info, errors.New(info.ErrMsg)
41+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
4342
}
4443
return info, nil
4544
}
@@ -52,7 +51,7 @@ func (r *Client) ReceptionistDel(options ReceptionistOptions) (info Receptionist
5251
}
5352
_ = json.Unmarshal(data, &info)
5453
if info.ErrCode != 0 {
55-
return info, errors.New(info.ErrMsg)
54+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
5655
}
5756
return info, nil
5857
}
@@ -74,7 +73,7 @@ func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err
7473
}
7574
_ = json.Unmarshal(data, &info)
7675
if info.ErrCode != 0 {
77-
return info, errors.New(info.ErrMsg)
76+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
7877
}
7978
return info, nil
8079
}

servicestate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -40,7 +39,7 @@ func (r *Client) ServiceStateGet(options ServiceStateGetOptions) (info ServiceSt
4039
}
4140
_ = json.Unmarshal(data, &info)
4241
if info.ErrCode != 0 {
43-
return info, errors.New(info.ErrMsg)
42+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
4443
}
4544
return info, nil
4645
}
@@ -61,7 +60,7 @@ func (r *Client) ServiceStateTrans(options ServiceStateTransOptions) (info BaseM
6160
}
6261
_ = json.Unmarshal(data, &info)
6362
if info.ErrCode != 0 {
64-
return info, errors.New(info.ErrMsg)
63+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
6564
}
6665
return info, nil
6766
}

token.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,47 @@ func (r *Client) GetAccessToken() (info AccessTokenSchema, err error) {
2626
return info, err
2727
}
2828
_ = json.Unmarshal(data, &info)
29-
fmt.Println(string(data))
3029
if info.ErrCode != 0 {
31-
return info, errors.New(info.ErrMsg)
30+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
3231
}
3332
return info, nil
3433
}
3534

35+
// RefreshAccessToken 刷新调用凭证access_token
36+
func (r *Client) RefreshAccessToken() error {
37+
//初始化AccessToken
38+
tokenInfo, err := r.GetAccessToken()
39+
if err != nil {
40+
return err
41+
}
42+
if err = r.setAccessToken(tokenInfo.AccessToken); err != nil {
43+
return err
44+
}
45+
r.accessToken = tokenInfo.AccessToken
46+
return nil
47+
}
48+
49+
func (r *Client) initAccessToken() error {
50+
//判断是否已初始化完成,如果己初始化则直接返回当前实例
51+
token, err := r.getAccessToken()
52+
if err != nil {
53+
return errors.New("cache unavailable")
54+
}
55+
if token == "" {
56+
if err = r.RefreshAccessToken(); err != nil {
57+
return err
58+
}
59+
} else {
60+
r.accessToken = token
61+
}
62+
return nil
63+
}
64+
65+
3666
func (r *Client) getAccessToken() (string, error) {
3767
return r.cache.Get("wechat:kf:" + r.corpID)
3868
}
3969

4070
func (r *Client) setAccessToken(token string) error {
4171
return r.cache.Set("wechat:kf:" + r.corpID, token, r.expireTime)
42-
}
72+
}

upgrade.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
87
)
@@ -36,7 +35,7 @@ func (r *Client) UpgradeServiceConfig() (info UpgradeServiceConfigSchema, err er
3635
}
3736
_ = json.Unmarshal(data, &info)
3837
if info.ErrCode != 0 {
39-
return info, errors.New(info.ErrMsg)
38+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
4039
}
4140
return info, nil
4241
}
@@ -64,7 +63,7 @@ func (r *Client) UpgradeService(options UpgradeServiceOptions) (info BaseModel,
6463
}
6564
_ = json.Unmarshal(data, &info)
6665
if info.ErrCode != 0 {
67-
return info, errors.New(info.ErrMsg)
66+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
6867
}
6968
return info, nil
7069
}
@@ -83,7 +82,7 @@ func (r *Client) UpgradeServiceCancel(options UpgradeServiceCancelOptions) (info
8382
}
8483
_ = json.Unmarshal(data, &info)
8584
if info.ErrCode != 0 {
86-
return info, errors.New(info.ErrMsg)
85+
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
8786
}
8887
return info, nil
8988
}

0 commit comments

Comments
 (0)