From 227d37bf3afc67aa8ea720a297968d255a15d27f Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 23 Jan 2019 10:43:40 +0800 Subject: [PATCH] - return result even error in dada api --- platformapi/dadaapi/dadaapi.go | 14 ++++++++------ platformapi/dadaapi/shop.go | 10 +++++----- platformapi/platformapi.go | 3 ++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/platformapi/dadaapi/dadaapi.go b/platformapi/dadaapi/dadaapi.go index 2812e84e..c5518944 100644 --- a/platformapi/dadaapi/dadaapi.go +++ b/platformapi/dadaapi/dadaapi.go @@ -18,11 +18,13 @@ const ( ) const ( - ResponseCodeException = -1 - ResponseCodeSuccess = 0 - ResponseCodeSignErr = 2003 - ResponseCodeRetryLater = 2012 - ResponseCodeNetworkErr = 2455 + ResponseCodeException = -1 + ResponseCodeSuccess = 0 + ResponseCodeSignErr = 2003 + ResponseCodeRetryLater = 2012 + ResponseCodeShopNotExist = 2402 + ResponseCodeShopAlreadyExist = 2403 + ResponseCodeNetworkErr = 2455 ) type API struct { @@ -124,8 +126,8 @@ func (a *API) AccessAPI(action string, params interface{}) (retVal *ResponseResu Msg: jsonResult1["msg"].(string), Status: jsonResult1["status"].(string), } + retVal.Result = jsonResult1["result"] if code == ResponseCodeSuccess { - retVal.Result = jsonResult1["result"] return platformapi.ErrLevelSuccess, nil } newErr := utils.NewErrorIntCode(retVal.Msg, code) diff --git a/platformapi/dadaapi/shop.go b/platformapi/dadaapi/shop.go index d423f38f..22abad34 100644 --- a/platformapi/dadaapi/shop.go +++ b/platformapi/dadaapi/shop.go @@ -50,8 +50,11 @@ func (a *API) ShopAdd(originShopID, stationName string, business int, cityName, if addParams != nil { params = utils.MergeMaps(params, addParams) } - successList, _, err := a.BatchShopAdd([]map[string]interface{}{params}) + successList, failedList, err := a.BatchShopAdd([]map[string]interface{}{params}) if err != nil { + if len(failedList) == 1 { + return "", utils.NewErrorIntCode(failedList[0]["msg"].(string), int(utils.MustInterface2Int64(failedList[0]["code"]))) + } return "", err } return utils.Interface2String(successList[0]["originShopId"]), nil @@ -59,9 +62,6 @@ func (a *API) ShopAdd(originShopID, stationName string, business int, cityName, func (a *API) BatchShopAdd(shopInfoList []map[string]interface{}) (successList, failedList []map[string]interface{}, err error) { result, err := a.AccessAPI("api/shop/add", shopInfoList) - if err != nil { - return nil, nil, err - } mapResult := result.Result.(map[string]interface{}) if successList2 := mapResult["successList"]; successList2 != nil { successList = utils.Slice2MapSlice(successList2.([]interface{})) @@ -69,7 +69,7 @@ func (a *API) BatchShopAdd(shopInfoList []map[string]interface{}) (successList, if failedList2 := mapResult["failedList"]; failedList2 != nil { failedList = utils.Slice2MapSlice(failedList2.([]interface{})) } - return successList, failedList, nil + return successList, failedList, err } func (a *API) ShopUpdate(originShopID string, shopInfo map[string]interface{}) (err error) { diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index 6cabcb26..908cd5b9 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -132,6 +132,7 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http. } else { errLevel, err = handleResponse(bodyMap) } + // baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry url:%v, request:%v, response:%s", request.URL, getClonedData(savedBuf), utils.Format4Output(bodyMap, true)) if err == nil { return nil } else if errLevel == ErrLevelExceedLimit { @@ -146,7 +147,7 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http. continue } } - baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry failed, url:%v, request:%v, response:%v, error:%v", request.URL, getClonedData(savedBuf), response, err) + baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry failed, url:%v, request:%v, response:%s, error:%v", request.URL, getClonedData(savedBuf), utils.Format4Output(bodyMap, true), err) return err } }