Skip to content

Commit

Permalink
polish:优化全局错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
NICEXAI committed Jul 27, 2021
1 parent 870c4ae commit 8814bbd
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 45 deletions.
11 changes: 5 additions & 6 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, e
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -58,7 +57,7 @@ func (r *Client) AccountDel(options AccountDelOptions) (info BaseModel, err erro
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -78,7 +77,7 @@ func (r *Client) AccountUpdate(options AccountUpdateOptions) (info BaseModel, er
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -104,7 +103,7 @@ func (r *Client) AccountList() (info AccountListSchema, err error) {
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -129,7 +128,7 @@ func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWay
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
21 changes: 2 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,8 @@ func New(options Options) (client *Client, err error) {
mutex: sync.Mutex{},
}

//判断是否已初始化完成,如果己初始化则直接返回当前实例
token, err := client.getAccessToken()
if err != nil {
return nil, errors.New("cache unavailable")
}

if token == "" {
//初始化AccessToken
tokenInfo, err := client.GetAccessToken()
if err != nil {
return nil, err
}

if err = client.setAccessToken(tokenInfo.AccessToken); err != nil {
return nil, err
}
client.accessToken = tokenInfo.AccessToken
} else {
client.accessToken = token
if err = client.initAccessToken(); err != nil {
return nil, err
}

return client, nil
Expand Down
3 changes: 1 addition & 2 deletions customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func (r *Client) CustomerBatchGet(options CustomerBatchGetOptions) (info Custome
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
22 changes: 22 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package WeChatCustomerServiceSDK

import "fmt"

// Error 错误
type Error struct {
ErrCode int `json:"err_code,omitempty"`
ErrMsg string `json:"err_msg"`
}

//输出错误信息
func (r Error) Error() string {
return fmt.Sprintf("%d:%s", r.ErrCode, r.ErrMsg)
}

// NewSDKErr 初始化SDK实例错误信息
func NewSDKErr(code int, msg string) Error {
return Error{
ErrCode: code,
ErrMsg: msg,
}
}
3 changes: 1 addition & 2 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
"mime/multipart"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (r *Client) MediaUpload(options MediaUploadOptions) (info MediaUploadSchema
_ = json.Unmarshal(data, &info)
fmt.Println(string(data))
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
3 changes: 1 addition & 2 deletions sendmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand All @@ -26,7 +25,7 @@ func (r *Client) SendMsg(options interface{}) (info SendMsgSchema, err error) {
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
7 changes: 3 additions & 4 deletions servicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info Receptionist
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -52,7 +51,7 @@ func (r *Client) ReceptionistDel(options ReceptionistOptions) (info Receptionist
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -74,7 +73,7 @@ func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
5 changes: 2 additions & 3 deletions servicestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func (r *Client) ServiceStateGet(options ServiceStateGetOptions) (info ServiceSt
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -61,7 +60,7 @@ func (r *Client) ServiceStateTrans(options ServiceStateTransOptions) (info BaseM
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
36 changes: 33 additions & 3 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,47 @@ func (r *Client) GetAccessToken() (info AccessTokenSchema, err error) {
return info, err
}
_ = json.Unmarshal(data, &info)
fmt.Println(string(data))
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}

// RefreshAccessToken 刷新调用凭证access_token
func (r *Client) RefreshAccessToken() error {
//初始化AccessToken
tokenInfo, err := r.GetAccessToken()
if err != nil {
return err
}
if err = r.setAccessToken(tokenInfo.AccessToken); err != nil {
return err
}
r.accessToken = tokenInfo.AccessToken
return nil
}

func (r *Client) initAccessToken() error {
//判断是否已初始化完成,如果己初始化则直接返回当前实例
token, err := r.getAccessToken()
if err != nil {
return errors.New("cache unavailable")
}
if token == "" {
if err = r.RefreshAccessToken(); err != nil {
return err
}
} else {
r.accessToken = token
}
return nil
}


func (r *Client) getAccessToken() (string, error) {
return r.cache.Get("wechat:kf:" + r.corpID)
}

func (r *Client) setAccessToken(token string) error {
return r.cache.Set("wechat:kf:" + r.corpID, token, r.expireTime)
}
}
7 changes: 3 additions & 4 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package WeChatCustomerServiceSDK

import (
"encoding/json"
"errors"
"fmt"
"github.com/NICEXAI/WeChatCustomerServiceSDK/util"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func (r *Client) UpgradeServiceConfig() (info UpgradeServiceConfigSchema, err er
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand Down Expand Up @@ -64,7 +63,7 @@ func (r *Client) UpgradeService(options UpgradeServiceOptions) (info BaseModel,
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}
Expand All @@ -83,7 +82,7 @@ func (r *Client) UpgradeServiceCancel(options UpgradeServiceCancelOptions) (info
}
_ = json.Unmarshal(data, &info)
if info.ErrCode != 0 {
return info, errors.New(info.ErrMsg)
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
}
return info, nil
}

0 comments on commit 8814bbd

Please sign in to comment.