From d28ca4f334efa19fc2ad148b1689577c2fe7d008 Mon Sep 17 00:00:00 2001 From: markwang <2951177317@qq.com> Date: Tue, 16 Apr 2024 10:51:51 +0800 Subject: [PATCH] feat: optimized-error-handling&remove unused constant (#772) * feat: optimized-error-handling&remove unused constant * feat: optimized-error-handling --------- Co-authored-by: markwang --- miniprogram/auth/auth.go | 6 ++--- miniprogram/business/phone_number.go | 7 +---- miniprogram/privacy/privacy.go | 18 +++---------- miniprogram/security/security.go | 14 ++-------- miniprogram/shortlink/shortlink.go | 6 +---- miniprogram/subscribe/subscribe.go | 6 +---- miniprogram/urllink/urllink.go | 5 +--- miniprogram/urlscheme/query.go | 6 +---- miniprogram/urlscheme/urlscheme.go | 5 +--- officialaccount/basic/short_url.go | 7 ++--- officialaccount/customerservice/manager.go | 12 ++------- officialaccount/datacube/publisher.go | 3 --- officialaccount/draft/draft.go | 20 +++----------- officialaccount/freepublish/freepublish.go | 14 ++-------- officialaccount/message/subscribe.go | 31 ++++------------------ officialaccount/message/template.go | 12 ++------- officialaccount/user/migrate.go | 4 --- officialaccount/user/tag.go | 8 +----- openplatform/context/accessToken.go | 7 ++--- 19 files changed, 33 insertions(+), 158 deletions(-) diff --git a/miniprogram/auth/auth.go b/miniprogram/auth/auth.go index 32c14934c..b0af59b5d 100644 --- a/miniprogram/auth/auth.go +++ b/miniprogram/auth/auth.go @@ -138,10 +138,8 @@ func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (*Get } var result GetPhoneNumberResponse - if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil { - return nil, err - } - return &result, nil + err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber") + return &result, err } // GetPhoneNumber 小程序通过code获取用户手机号 diff --git a/miniprogram/business/phone_number.go b/miniprogram/business/phone_number.go index bf99057ea..729214990 100644 --- a/miniprogram/business/phone_number.go +++ b/miniprogram/business/phone_number.go @@ -45,10 +45,5 @@ func (business *Business) GetPhoneNumber(in *GetPhoneNumberRequest) (info PhoneI PhoneInfo PhoneInfo `json:"phone_info"` } err = util.DecodeWithError(response, &resp, "business.GetPhoneNumber") - if nil != err { - return - } - - info = resp.PhoneInfo - return + return resp.PhoneInfo, err } diff --git a/miniprogram/privacy/privacy.go b/miniprogram/privacy/privacy.go index 024e379da..176dfc50e 100644 --- a/miniprogram/privacy/privacy.go +++ b/miniprogram/privacy/privacy.go @@ -103,11 +103,8 @@ func (s *Privacy) GetPrivacySetting(privacyVer int) (GetPrivacySettingResponse, } // 返回错误信息 var result GetPrivacySettingResponse - if err = util.DecodeWithError(response, &result, "getprivacysetting"); err != nil { - return GetPrivacySettingResponse{}, err - } - - return result, nil + err = util.DecodeWithError(response, &result, "getprivacysetting") + return result, err } // SetPrivacySetting 更新小程序权限配置 @@ -130,11 +127,7 @@ func (s *Privacy) SetPrivacySetting(privacyVer int, ownerSetting OwnerSetting, s } // 返回错误信息 - if err = util.DecodeWithCommonError(response, "setprivacysetting"); err != nil { - return err - } - - return err + return util.DecodeWithCommonError(response, "setprivacysetting") } // UploadPrivacyExtFileResponse 上传权限定义模板响应参数 @@ -159,9 +152,6 @@ func (s *Privacy) UploadPrivacyExtFile(fileData []byte) (UploadPrivacyExtFileRes // 返回错误信息 var result UploadPrivacyExtFileResponse - if err = util.DecodeWithError(response, &result, "setprivacysetting"); err != nil { - return UploadPrivacyExtFileResponse{}, err - } - + err = util.DecodeWithError(response, &result, "setprivacysetting") return result, err } diff --git a/miniprogram/security/security.go b/miniprogram/security/security.go index 7e4d5dc92..3e979a513 100644 --- a/miniprogram/security/security.go +++ b/miniprogram/security/security.go @@ -51,12 +51,7 @@ func (security *Security) MediaCheckAsyncV1(in *MediaCheckAsyncV1Request) (trace TraceID string `json:"trace_id"` } err = util.DecodeWithError(response, &res, "MediaCheckAsyncV1") - if err != nil { - return - } - - traceID = res.TraceID - return + return res.TraceID, err } // MediaCheckAsyncRequest 图片/音频异步校验请求参数 @@ -93,12 +88,7 @@ func (security *Security) MediaCheckAsync(in *MediaCheckAsyncRequest) (traceID s TraceID string `json:"trace_id"` } err = util.DecodeWithError(response, &res, "MediaCheckAsync") - if err != nil { - return - } - - traceID = res.TraceID - return + return res.TraceID, err } // ImageCheckV1 校验一张图片是否含有违法违规内容(同步) diff --git a/miniprogram/shortlink/shortlink.go b/miniprogram/shortlink/shortlink.go index c386c21ca..f55b89bfa 100644 --- a/miniprogram/shortlink/shortlink.go +++ b/miniprogram/shortlink/shortlink.go @@ -60,11 +60,7 @@ func (shortLink *ShortLink) generate(shortLinkParams ShortLinker) (string, error // 使用通用方法返回错误 var res resShortLinker err = util.DecodeWithError(response, &res, "GenerateShortLink") - if err != nil { - return "", err - } - - return res.Link, nil + return res.Link, err } // GenerateShortLinkPermanent 生成永久 shortLink diff --git a/miniprogram/subscribe/subscribe.go b/miniprogram/subscribe/subscribe.go index 0015a4365..9099044a1 100644 --- a/miniprogram/subscribe/subscribe.go +++ b/miniprogram/subscribe/subscribe.go @@ -168,11 +168,7 @@ func (s *Subscribe) Add(ShortID string, kidList []int, sceneDesc string) (templa } var result resSubscribeAdd err = util.DecodeWithError(response, &result, "AddSubscribe") - if err != nil { - return - } - templateID = result.TemplateID - return + return result.TemplateID, err } // Delete 删除私有模板 diff --git a/miniprogram/urllink/urllink.go b/miniprogram/urllink/urllink.go index 378d58afb..56975a685 100644 --- a/miniprogram/urllink/urllink.go +++ b/miniprogram/urllink/urllink.go @@ -65,8 +65,5 @@ func (u *URLLink) Generate(params *ULParams) (string, error) { } var resp ULResult err = util.DecodeWithError(response, &resp, "URLLink.Generate") - if err != nil { - return "", err - } - return resp.URLLink, nil + return resp.URLLink, err } diff --git a/miniprogram/urlscheme/query.go b/miniprogram/urlscheme/query.go index 79b37fbe0..72506e83b 100644 --- a/miniprogram/urlscheme/query.go +++ b/miniprogram/urlscheme/query.go @@ -62,9 +62,5 @@ func (u *URLScheme) QueryScheme(querySchemeParams QueryScheme) (schemeInfo Schem // 使用通用方法返回错误 var res resQueryScheme err = util.DecodeWithError(response, &res, "QueryScheme") - if err != nil { - return - } - - return res.SchemeInfo, res.VisitOpenid, nil + return res.SchemeInfo, res.VisitOpenid, err } diff --git a/miniprogram/urlscheme/urlscheme.go b/miniprogram/urlscheme/urlscheme.go index 37bcd753d..2659cfba2 100644 --- a/miniprogram/urlscheme/urlscheme.go +++ b/miniprogram/urlscheme/urlscheme.go @@ -78,8 +78,5 @@ func (u *URLScheme) Generate(params *USParams) (string, error) { } var resp USResult err = util.DecodeWithError(response, &resp, "URLScheme.Generate") - if err != nil { - return "", err - } - return resp.OpenLink, nil + return resp.OpenLink, err } diff --git a/officialaccount/basic/short_url.go b/officialaccount/basic/short_url.go index 94be9bb88..a3a1440c8 100644 --- a/officialaccount/basic/short_url.go +++ b/officialaccount/basic/short_url.go @@ -44,9 +44,6 @@ func (basic *Basic) Long2ShortURL(longURL string) (shortURL string, err error) { if err != nil { return } - if err = util.DecodeWithError(responseBytes, resp, long2shortAction); err != nil { - return - } - shortURL = resp.ShortURL - return + err = util.DecodeWithError(responseBytes, resp, long2shortAction) + return resp.ShortURL, err } diff --git a/officialaccount/customerservice/manager.go b/officialaccount/customerservice/manager.go index 6422fb74d..973a86c2f 100644 --- a/officialaccount/customerservice/manager.go +++ b/officialaccount/customerservice/manager.go @@ -72,11 +72,7 @@ func (csm *Manager) List() (customerServiceList []*KeFuInfo, err error) { } var res resKeFuList err = util.DecodeWithError(response, &res, "ListCustomerService") - if err != nil { - return - } - customerServiceList = res.KfList - return + return res.KfList, err } // KeFuOnlineInfo 客服在线信息 @@ -107,11 +103,7 @@ func (csm *Manager) OnlineList() (customerServiceOnlineList []*KeFuOnlineInfo, e } var res resKeFuOnlineList err = util.DecodeWithError(response, &res, "ListOnlineCustomerService") - if err != nil { - return - } - customerServiceOnlineList = res.KfOnlineList - return + return res.KfOnlineList, err } // Add 添加客服账号 diff --git a/officialaccount/datacube/publisher.go b/officialaccount/datacube/publisher.go index bd640d0ac..73997cf7e 100644 --- a/officialaccount/datacube/publisher.go +++ b/officialaccount/datacube/publisher.go @@ -183,9 +183,6 @@ func (cube *DataCube) fetchData(params ParamsPublisher) (response []byte, err er uri := fmt.Sprintf("%s?%s", publisherURL, v.Encode()) response, err = util.HTTPGet(uri) - if err != nil { - return - } return } diff --git a/officialaccount/draft/draft.go b/officialaccount/draft/draft.go index e7f15d303..76a37515e 100644 --- a/officialaccount/draft/draft.go +++ b/officialaccount/draft/draft.go @@ -64,11 +64,7 @@ func (draft *Draft) AddDraft(articles []*Article) (mediaID string, err error) { MediaID string `json:"media_id"` } err = util.DecodeWithError(response, &res, "AddDraft") - if err != nil { - return - } - mediaID = res.MediaID - return + return res.MediaID, err } // GetDraft 获取草稿 @@ -94,12 +90,7 @@ func (draft *Draft) GetDraft(mediaID string) (articles []*Article, err error) { NewsItem []*Article `json:"news_item"` } err = util.DecodeWithError(response, &res, "GetDraft") - if err != nil { - return - } - - articles = res.NewsItem - return + return res.NewsItem, err } // DeleteDraft 删除草稿 @@ -172,12 +163,7 @@ func (draft *Draft) CountDraft() (total uint, err error) { Total uint `json:"total_count"` } err = util.DecodeWithError(response, &res, "CountDraft") - if nil != err { - return - } - - total = res.Total - return + return res.Total, err } // ArticleList 草稿列表 diff --git a/officialaccount/freepublish/freepublish.go b/officialaccount/freepublish/freepublish.go index 041bf6612..7c414a80d 100644 --- a/officialaccount/freepublish/freepublish.go +++ b/officialaccount/freepublish/freepublish.go @@ -73,12 +73,7 @@ func (freePublish *FreePublish) Publish(mediaID string) (publishID int64, err er PublishID int64 `json:"publish_id"` } err = util.DecodeWithError(response, &res, "SubmitFreePublish") - if err != nil { - return - } - - publishID = res.PublishID - return + return res.PublishID, err } // PublishStatusList 发布任务状态列表 @@ -191,12 +186,7 @@ func (freePublish *FreePublish) First(articleID string) (list []Article, err err NewsItem []Article `json:"news_item"` } err = util.DecodeWithError(response, &res, "FirstFreePublish") - if err != nil { - return - } - - list = res.NewsItem - return + return res.NewsItem, err } // ArticleList 发布列表 diff --git a/officialaccount/message/subscribe.go b/officialaccount/message/subscribe.go index 9b6e18ace..c0e9516fa 100644 --- a/officialaccount/message/subscribe.go +++ b/officialaccount/message/subscribe.go @@ -90,11 +90,7 @@ func (tpl *Subscribe) List() (templateList []*PrivateSubscribeItem, err error) { } var res resPrivateSubscribeList err = util.DecodeWithError(response, &res, "ListSubscribe") - if err != nil { - return - } - templateList = res.SubscriptionList - return + return res.SubscriptionList, err } type resSubscribeAdd struct { @@ -123,11 +119,7 @@ func (tpl *Subscribe) Add(ShortID string, kidList []int, sceneDesc string) (temp } var result resSubscribeAdd err = util.DecodeWithError(response, &result, "AddSubscribe") - if err != nil { - return - } - templateID = result.TemplateID - return + return result.TemplateID, err } // Delete 删除私有模板 @@ -175,11 +167,7 @@ func (tpl *Subscribe) GetCategory() (categoryList []*PublicTemplateCategory, err } var result resSubscribeCategoryList err = util.DecodeWithError(response, &result, "GetCategory") - if err != nil { - return - } - categoryList = result.CategoryList - return + return result.CategoryList, err } // PublicTemplateKeyWords 模板中的关键词 @@ -210,11 +198,7 @@ func (tpl *Subscribe) GetPubTplKeyWordsByID(titleID string) (keyWordsList []*Pub } var result resPublicTemplateKeyWordsList err = util.DecodeWithError(response, &result, "GetPublicTemplateKeyWords") - if err != nil { - return - } - keyWordsList = result.KeyWordsList - return + return result.KeyWordsList, err } // PublicTemplateTitle 类目下的公共模板 @@ -246,10 +230,5 @@ func (tpl *Subscribe) GetPublicTemplateTitleList(ids string, start int, limit in } var result resPublicTemplateTitleList err = util.DecodeWithError(response, &result, "GetPublicTemplateTitle") - if err != nil { - return - } - count = result.Count - templateTitleList = result.TemplateTitleList - return + return result.Count, result.TemplateTitleList, err } diff --git a/officialaccount/message/template.go b/officialaccount/message/template.go index 24a47ba26..1c657ea92 100644 --- a/officialaccount/message/template.go +++ b/officialaccount/message/template.go @@ -111,11 +111,7 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) { } var res resTemplateList err = util.DecodeWithError(response, &res, "ListTemplate") - if err != nil { - return - } - templateList = res.TemplateList - return + return res.TemplateList, err } type resTemplateAdd struct { @@ -143,11 +139,7 @@ func (tpl *Template) Add(shortID string) (templateID string, err error) { var result resTemplateAdd err = util.DecodeWithError(response, &result, "AddTemplate") - if err != nil { - return - } - templateID = result.TemplateID - return + return result.TemplateID, err } // Delete 删除私有模板. diff --git a/officialaccount/user/migrate.go b/officialaccount/user/migrate.go index 44afe05a2..cac6a1e59 100644 --- a/officialaccount/user/migrate.go +++ b/officialaccount/user/migrate.go @@ -62,10 +62,6 @@ func (user *User) ListChangeOpenIDs(fromAppID string, openIDs ...string) (list * } err = util.DecodeWithError(resp, list, "ListChangeOpenIDs") - if err != nil { - return - } - return } diff --git a/officialaccount/user/tag.go b/officialaccount/user/tag.go index 3a61c5dac..6353cfc63 100644 --- a/officialaccount/user/tag.go +++ b/officialaccount/user/tag.go @@ -126,10 +126,7 @@ func (user *User) GetTag() (tags []*TagInfo, err error) { Tags []*TagInfo `json:"tags"` } err = json.Unmarshal(response, &result) - if err != nil { - return - } - return result.Tags, nil + return result.Tags, err } // OpenIDListByTag 获取标签下粉丝列表 @@ -154,9 +151,6 @@ func (user *User) OpenIDListByTag(tagID int32, nextOpenID ...string) (userList * } userList = new(TagOpenIDList) err = json.Unmarshal(response, &userList) - if err != nil { - return - } return } diff --git a/openplatform/context/accessToken.go b/openplatform/context/accessToken.go index 68861295f..465eb170a 100644 --- a/openplatform/context/accessToken.go +++ b/openplatform/context/accessToken.go @@ -100,11 +100,8 @@ func (ctx *Context) GetPreCodeContext(stdCtx context.Context) (string, error) { var ret struct { PreCode string `json:"pre_auth_code"` } - if err := json.Unmarshal(body, &ret); err != nil { - return "", err - } - - return ret.PreCode, nil + err = json.Unmarshal(body, &ret) + return ret.PreCode, err } // GetPreCode 获取预授权码