From 26f1d37caf47ca16b971eac0674d96c9ce23e021 Mon Sep 17 00:00:00 2001 From: gazebo Date: Sun, 17 Mar 2019 21:41:45 +0800 Subject: [PATCH] - refactor RebuildError - output store and sku info when mtwm api failed --- platformapi/ebaiapi/ebaiapi.go | 24 ++++++------------------ platformapi/jdapi/jdapi.go | 34 +++++++++------------------------- platformapi/mtwmapi/mtwmapi.go | 5 +++++ platformapi/platformapi.go | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/platformapi/ebaiapi/ebaiapi.go b/platformapi/ebaiapi/ebaiapi.go index d51071aa..cc7a047b 100644 --- a/platformapi/ebaiapi/ebaiapi.go +++ b/platformapi/ebaiapi/ebaiapi.go @@ -128,23 +128,11 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon } return platformapi.ErrLevelCodeIsNotOK, newErr }) - if err != nil { - if codeErr, ok := err.(*utils.ErrorWithCode); ok { - appendErrList := []string{} - for _, key := range []string{ - KeyBaiduShopID, - KeyShopID, - KeySkuID, - KeyCustomSkuID, - } { - if body[key] != nil { - appendErrList = append(appendErrList, fmt.Sprintf("[%s:%v]", key, body[key])) - } - } - if len(appendErrList) > 0 { - err = utils.NewErrorCode(strings.Join(appendErrList, ",")+", "+codeErr.ErrMsg(), codeErr.Code()) - } - } - } + err = platformapi.RebuildError(err, body, []string{ + KeyBaiduShopID, + KeyShopID, + KeySkuID, + KeyCustomSkuID, + }) return retVal, err } diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 421e980b..c0bf14f4 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -101,6 +101,11 @@ var ( var ( nullResultParser = genNoPageResultParser("code", "msg", "", "0") + watchKeys = []string{ + KeyOutStationNo, + KeyStationNo, + "StoreNo", + } ) type PageResultParser func(map[string]interface{}, int) ([]interface{}, int, error) @@ -196,28 +201,7 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal return platformapi.ErrLevelCodeIsNotOK, newErr } }) - return retVal, a.rebuildError(err, jdParams) -} - -func (a *API) rebuildError(inErr error, jdParams map[string]interface{}) (outErr error) { - if inErr != nil { - if codeErr, ok := inErr.(*utils.ErrorWithCode); ok { - appendErrList := []string{} - for _, key := range []string{ - KeyOutStationNo, - KeyStationNo, - "StoreNo", - } { - if jdParams[key] != nil { - appendErrList = append(appendErrList, fmt.Sprintf("[%s:%v]", key, jdParams[key])) - } - } - if len(appendErrList) > 0 { - inErr = utils.NewErrorCode(strings.Join(appendErrList, ",")+", "+codeErr.ErrMsg(), codeErr.Code()) - } - } - } - return inErr + return retVal, platformapi.RebuildError(err, jdParams, watchKeys) } func genNoPageResultParser(codeKey, msgKey, resultKey, okCode string) func(data map[string]interface{}) (interface{}, error) { @@ -269,7 +253,7 @@ func (a *API) AccessAPINoPage(apiStr string, jdParams map[string]interface{}, ke if err == nil { return utils.DictKeysMan(result, keyToRemove, keyToKeep), nil } - err = a.rebuildError(err, jdParams) + err = platformapi.RebuildError(err, jdParams, watchKeys) baseapi.SugarLogger.Infof("AccessAPINoPage failed, apiStr:%s, jdParams:%s, data:%s, error:%v", apiStr, utils.Format4Output(jdParams, true), utils.Format4Output(jsonResult, true), err) return result, err } @@ -367,12 +351,12 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{}, if innerCode != "0" { err2 := utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1) baseapi.SugarLogger.Infof("AccessAPIHavePage failed, apiStr:%s, jdParams:%s, data:%s, error:%v", apiStr, utils.Format4Output(jdParams, true), utils.Format4Output(jsonResult, true), err2) - return nil, totalCount, a.rebuildError(err2, jdParams) + return nil, totalCount, platformapi.RebuildError(err2, jdParams, watchKeys) } inResult, totalCount2, err := pageResultParser(data, totalCount) if err != nil { - return nil, totalCount, a.rebuildError(err, jdParams) + return nil, totalCount, platformapi.RebuildError(err, jdParams, watchKeys) } totalCount = totalCount2 diff --git a/platformapi/mtwmapi/mtwmapi.go b/platformapi/mtwmapi/mtwmapi.go index 2cd99388..6ffc8dae 100644 --- a/platformapi/mtwmapi/mtwmapi.go +++ b/platformapi/mtwmapi/mtwmapi.go @@ -139,5 +139,10 @@ func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{} retVal = jsonResult1["data"] return platformapi.ErrLevelSuccess, nil }) + err = platformapi.RebuildError(err, bizParams, []string{ + KeyAppPoiCode, + KeyAppPoiCodes, + KeyAppFoodCode, + }) return retVal, err } diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index fd3b48d3..731f9c3e 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -3,6 +3,7 @@ package platformapi import ( "bytes" "errors" + "fmt" "io" "io/ioutil" "net" @@ -173,3 +174,20 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http. return err } } + +func RebuildError(inErr error, bzParams map[string]interface{}, watchKeys []string) (outErr error) { + if inErr != nil { + if codeErr, ok := inErr.(*utils.ErrorWithCode); ok { + appendErrList := []string{} + for _, key := range watchKeys { + if bzParams[key] != nil { + appendErrList = append(appendErrList, fmt.Sprintf("[%s:%v]", key, bzParams[key])) + } + } + if len(appendErrList) > 0 { + inErr = utils.NewErrorCode(strings.Join(appendErrList, ",")+", "+codeErr.ErrMsg(), codeErr.Code()) + } + } + } + return inErr +}