- 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

@@ -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