- refactor RebuildError

- output store and sku info when mtwm api failed
This commit is contained in:
gazebo
2019-03-17 21:41:45 +08:00
parent 1cec54c840
commit 26f1d37caf
4 changed files with 38 additions and 43 deletions

View File

@@ -128,23 +128,11 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
} }
return platformapi.ErrLevelCodeIsNotOK, newErr return platformapi.ErrLevelCodeIsNotOK, newErr
}) })
if err != nil { err = platformapi.RebuildError(err, body, []string{
if codeErr, ok := err.(*utils.ErrorWithCode); ok { KeyBaiduShopID,
appendErrList := []string{} KeyShopID,
for _, key := range []string{ KeySkuID,
KeyBaiduShopID, KeyCustomSkuID,
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())
}
}
}
return retVal, err return retVal, err
} }

View File

@@ -101,6 +101,11 @@ var (
var ( var (
nullResultParser = genNoPageResultParser("code", "msg", "", "0") nullResultParser = genNoPageResultParser("code", "msg", "", "0")
watchKeys = []string{
KeyOutStationNo,
KeyStationNo,
"StoreNo",
}
) )
type PageResultParser func(map[string]interface{}, int) ([]interface{}, int, error) 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 platformapi.ErrLevelCodeIsNotOK, newErr
} }
}) })
return retVal, a.rebuildError(err, jdParams) return retVal, platformapi.RebuildError(err, jdParams, watchKeys)
}
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
} }
func genNoPageResultParser(codeKey, msgKey, resultKey, okCode string) func(data map[string]interface{}) (interface{}, error) { 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 { if err == nil {
return utils.DictKeysMan(result, keyToRemove, keyToKeep), 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) 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 return result, err
} }
@@ -367,12 +351,12 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{},
if innerCode != "0" { if innerCode != "0" {
err2 := utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1) 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) 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) inResult, totalCount2, err := pageResultParser(data, totalCount)
if err != nil { if err != nil {
return nil, totalCount, a.rebuildError(err, jdParams) return nil, totalCount, platformapi.RebuildError(err, jdParams, watchKeys)
} }
totalCount = totalCount2 totalCount = totalCount2

View File

@@ -139,5 +139,10 @@ func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{}
retVal = jsonResult1["data"] retVal = jsonResult1["data"]
return platformapi.ErrLevelSuccess, nil return platformapi.ErrLevelSuccess, nil
}) })
err = platformapi.RebuildError(err, bizParams, []string{
KeyAppPoiCode,
KeyAppPoiCodes,
KeyAppFoodCode,
})
return retVal, err return retVal, err
} }

View File

@@ -3,6 +3,7 @@ package platformapi
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
@@ -173,3 +174,20 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
return err 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
}