Skip to content

Commit eabb697

Browse files
author
breezeli
authored
Merge pull request TencentBlueKing#1766 from wusendong/solve_3.3
refactor: return error is not all success
2 parents a594bda + 87250c2 commit eabb697

File tree

25 files changed

+51
-39
lines changed

25 files changed

+51
-39
lines changed

resources/errors/common/default/common.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"1199010": "'%s' 未赋值",
1313
"1199011": "'%s' 参数无效或不存在",
1414
"1199013": "读取Data字段数据失败",
15-
"1199014": "数据唯一性校验失败,唯一组合 [%s] 重复",
15+
"1199014": "数据唯一性校验失败, %s 重复",
1616
"1199015": "'%s' 数据越界",
1717
"1199016": "字段正则校验失败",
1818
"1199017": "查询数据失败",
@@ -39,6 +39,6 @@
3939
"1199040": "转换%s模型的%s字段到 %s,错误:%s",
4040
"1199041": "处理%s,错误:%s",
4141
"1199043": "字段值校验不通过, %s",
42-
42+
"1199044": "未全部成功",
4343
"": ""
4444
}

resources/errors/english/en/common.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
"1199040": "convert %s model field %s to %s error %s",
3939
"1199041": "handle %s error %s",
4040
"1199043": "The field value check does not pass, %s",
41+
"1199044": "not all success",
4142
"":""
4243
}

src/api_server/service/v2/plat.go

-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package v2
1414

1515
import (
1616
"context"
17-
"strconv"
1817
"strings"
1918

2019
"configcenter/src/api_server/logics/v2/common/converter"
@@ -172,10 +171,6 @@ func (s *Service) createPlats(req *restful.Request, resp *restful.Response) {
172171
result, err := s.CoreAPI.HostServer().CreatePlat(context.Background(), pheader, param)
173172
if nil != err {
174173
blog.Errorf("createPlats error:%v", err)
175-
if strings.Contains(err.Error(), strconv.Itoa(common.CCErrCommDuplicateItem)) {
176-
converter.RespFailV2(common.CCErrCommDuplicateItem, defErr.Error(common.CCErrCommDuplicateItem).Error(), resp)
177-
return
178-
}
179174
converter.RespFailV2(common.CCErrCommHTTPDoRequestFailed, defErr.Error(common.CCErrCommHTTPDoRequestFailed).Error(), resp)
180175
return
181176
}

src/common/errInfo.go

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const (
146146

147147
// CCErrCommParamsNeedFloat the parameter must be float type
148148
CCErrCommParamsNeedFloat = 1199042
149+
CCErrCommNotAllSuccess = 1199044
149150

150151
// apiserver 1100XXX
151152

src/scene_server/datacollection/logics/device.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (lgc *Logics) UpdateDevice(pheader http.Header, netDeviceID uint64, deviceI
6262
// device name has been occupied
6363
if nil == err && deviceID != netDeviceID {
6464
blog.Errorf("[NetDevice] update net device fail, duplicate device name: [%s]", deviceInfo.DeviceName)
65-
return defErr.Error(common.CCErrCommDuplicateItem)
65+
return defErr.Errorf(common.CCErrCommDuplicateItem, "device")
6666
}
6767

6868
return lgc.updateDevice(pheader, deviceInfo, netDeviceID, util.GetOwnerID(pheader))
@@ -206,7 +206,7 @@ func (lgc *Logics) addDevice(pheader http.Header, deviceInfo meta.NetcollectDevi
206206
}
207207
if isExist {
208208
blog.Errorf("[NetDevice] add net device fail, error: duplicate device_name")
209-
return INVALIDID, defErr.Error(common.CCErrCommDuplicateItem)
209+
return INVALIDID, defErr.Errorf(common.CCErrCommDuplicateItem, "device")
210210
}
211211

212212
// add to the storage

src/scene_server/datacollection/logics/property.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (lgc *Logics) addProperty(pheader http.Header, netPropertyInfo meta.Netcoll
235235
}
236236
if isExist { // exist the same [deviceID + propertyID], duplicate data
237237
blog.Errorf("[NetProperty] add net property fail, error: duplicate propertyID and deviceID")
238-
return INVALIDID, defErr.Errorf(common.CCErrCommDuplicateItem)
238+
return INVALIDID, defErr.Errorf(common.CCErrCommDuplicateItem, "property_id+device_id")
239239
}
240240

241241
// add to the storage
@@ -278,7 +278,7 @@ func (lgc *Logics) updateProperty(
278278
blog.Errorf("[NetProperty] update net property fail, duplicate deviceID [%d] and propertyID [%s]",
279279
netPropertyInfo.DeviceID, netPropertyInfo.PropertyID)
280280

281-
return defErr.Error(common.CCErrCommDuplicateItem)
281+
return defErr.Errorf(common.CCErrCommDuplicateItem, "property_id+device_id")
282282
}
283283
}
284284

src/scene_server/event_server/service/subscription.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *Service) Subscribe(req *restful.Request, resp *restful.Response) {
6565
exists := []metadata.Subscription{}
6666
err = s.db.Table(common.BKTableNameSubscription).Find(map[string]interface{}{common.BKSubscriptionNameField: sub.SubscriptionName, common.BKOwnerIDField: ownerID}).All(s.ctx, &exists)
6767
if err != nil {
68-
resp.WriteError(http.StatusInternalServerError, &metadata.RespError{Msg: defErr.Error(common.CCErrCommDuplicateItem)})
68+
resp.WriteError(http.StatusInternalServerError, &metadata.RespError{Msg: defErr.Errorf(common.CCErrCommDuplicateItem, "subscription_name")})
6969
return
7070
}
7171

src/scene_server/host_server/service/phpapi.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
"net/http"
1919
"strings"
2020

21-
"github.com/emicklei/go-restful"
22-
2321
"configcenter/src/common"
2422
"configcenter/src/common/blog"
23+
"configcenter/src/common/errors"
2524
"configcenter/src/common/mapstr"
2625
meta "configcenter/src/common/metadata"
2726
"configcenter/src/common/util"
2827
"configcenter/src/scene_server/validator"
28+
29+
"github.com/emicklei/go-restful"
2930
)
3031

3132
// updateHostPlat 根据条件更新主机信息
@@ -802,9 +803,10 @@ func (s *Service) CreatePlat(req *restful.Request, resp *restful.Response) {
802803

803804
if nil != validErr {
804805
blog.Errorf("CreatePlat error: %v, input:%+v,rid:%s", validErr, input, srvData.rid)
805-
if strings.Contains(srvData.ccErr.Error(common.CCErrCommDuplicateItem).Error(), validErr.Error()) {
806-
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: srvData.ccErr.Error(common.CCErrCommDuplicateItem)})
807-
return
806+
if se, ok := validErr.(errors.CCErrorCoder); ok {
807+
if se.GetCode() == common.CCErrCommDuplicateItem {
808+
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: srvData.ccErr.Errorf(common.CCErrCommDuplicateItem, "")})
809+
}
808810
}
809811
resp.WriteError(http.StatusInternalServerError, &meta.RespError{Msg: srvData.ccErr.Error(common.CCErrTopoInstCreateFailed)})
810812
return

src/scene_server/topo_server/core/model/attribute.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (a *attribute) Update(data mapstr.MapStr) error {
204204
}
205205

206206
if exists {
207-
return a.params.Err.Error(common.CCErrCommDuplicateItem)
207+
return a.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
208208
}
209209

210210
input := metadata.UpdateOption{

src/scene_server/topo_server/core/model/classification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (cli *classification) Update(data mapstr.MapStr) error {
149149
}
150150

151151
if exists {
152-
return cli.params.Err.Error(common.CCErrCommDuplicateItem)
152+
return cli.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
153153
}
154154

155155
cond := condition.CreateCondition()

src/scene_server/topo_server/core/model/group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (g *group) Update(data mapstr.MapStr) error {
137137
}
138138

139139
if exists {
140-
return g.params.Err.Error(common.CCErrCommDuplicateItem)
140+
return g.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
141141
}
142142

143143
cond := condition.CreateCondition()

src/scene_server/topo_server/core/model/object.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (o *object) Create() error {
450450
}
451451

452452
if exists {
453-
return o.params.Err.Error(common.CCErrCommDuplicateItem)
453+
return o.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
454454
}
455455

456456
if o.obj.ObjIcon == "" {
@@ -489,7 +489,7 @@ func (o *object) Update(data mapstr.MapStr) error {
489489
}
490490

491491
if exists {
492-
return o.params.Err.Error(common.CCErrCommDuplicateItem)
492+
return o.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
493493
}
494494

495495
// update action

src/scene_server/topo_server/core/operation/import_association.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func (ia *importAssociation) isExistInstAsst(idx int, cond condition.Condition,
452452
}
453453
if rsp.Data.Info[0].AsstInstID != dstInstID &&
454454
asstMapping == metadata.OneToOneMapping {
455-
return false, ia.params.Err.Errorf(common.CCErrCommDuplicateItem)
455+
return false, ia.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
456456
}
457457

458458
return true, nil

src/scene_server/topo_server/core/operation/object.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
102102
}
103103

104104
result := mapstr.New()
105+
hasError := false
105106
for objID, inputData := range inputData {
106107
subResult := mapstr.New()
107108
if err := o.IsValidObject(params, objID); nil != err {
108109
blog.Errorf("not found the objid: %s", objID)
109110
subResult["errors"] = fmt.Sprintf("the object(%s) is invalid", objID)
110111
result[objID] = subResult
112+
hasError = true
111113
continue
112114
}
113115

@@ -122,6 +124,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
122124
blog.Errorf("not found the objid: %s", objID)
123125
subResult["errors"] = err.Error()
124126
result[objID] = subResult
127+
hasError = true
125128
continue
126129
}
127130

@@ -143,6 +146,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
143146
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err)
144147
subResult["errors"] = errStr
145148
result[objID] = subResult
149+
hasError = true
146150
continue
147151
}
148152

@@ -170,6 +174,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
170174
}
171175
}
172176
result[objID] = subResult
177+
hasError = true
173178
continue
174179
}
175180

@@ -190,6 +195,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
190195
}
191196
}
192197
result[objID] = subResult
198+
hasError = true
193199
continue
194200
}
195201
attrCond := condition.CreateCondition()
@@ -209,6 +215,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
209215
}
210216
}
211217
result[objID] = subResult
218+
hasError = true
212219
continue
213220
}
214221

@@ -227,6 +234,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
227234
}
228235
}
229236
result[objID] = subResult
237+
hasError = true
230238
continue
231239
}
232240

@@ -245,6 +253,7 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
245253
}
246254
}
247255
result[objID] = subResult
256+
hasError = true
248257
continue
249258
}
250259

@@ -264,7 +273,11 @@ func (o *object) CreateObjectBatch(params types.ContextParams, data mapstr.MapSt
264273

265274
}
266275

276+
if hasError {
277+
return result, params.Err.Error(common.CCErrCommNotAllSuccess)
278+
}
267279
return result, nil
280+
268281
}
269282
func (o *object) FindObjectBatch(params types.ContextParams, data mapstr.MapStr) (mapstr.MapStr, error) {
270283

@@ -333,7 +346,7 @@ func (o *object) CreateObject(params types.ContextParams, isMainline bool, data
333346

334347
if exists {
335348
blog.Errorf("[operation-obj] the object(%#v) is repeated", data)
336-
return nil, params.Err.Error(common.CCErrCommDuplicateItem)
349+
return nil, params.Err.Errorf(common.CCErrCommDuplicateItem, "")
337350
}
338351

339352
err = obj.Create()
@@ -694,7 +707,7 @@ func (o *object) UpdateObject(params types.ContextParams, data mapstr.MapStr, id
694707

695708
if exists {
696709
blog.Errorf("[operation-obj] the object(%#v) is repeated", data)
697-
return params.Err.Error(common.CCErrCommDuplicateItem)
710+
return params.Err.Errorf(common.CCErrCommDuplicateItem, "")
698711
}
699712
if err = obj.Update(data); nil != err {
700713
blog.Errorf("[operation-obj] failed to update the object(%d), the new data(%#v), err: %s", id, data, err.Error())

src/scene_server/topo_server/core/privilege/group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (u *userGroup) checkGroupNameRepeat(supplierAccount, groupID, groupName str
7878

7979
if 0 < len(rsp.Data) {
8080
blog.Warnf("[permission] the group name (%s) repeated", groupName)
81-
return u.params.Err.Error(common.CCErrCommDuplicateItem)
81+
return u.params.Err.Errorf(common.CCErrCommDuplicateItem, "")
8282
}
8383

8484
return nil

src/source_controller/coreservice/core/association/instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (m *associationInstance) CreateOneInstanceAssociation(ctx core.ContextParam
9898
}
9999
if exists {
100100
blog.Errorf("association instance (%#v)is duplicated", inputParam.Data)
101-
return nil, ctx.Error.Error(common.CCErrCommDuplicateItem)
101+
return nil, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
102102
}
103103
//check association kind
104104
cond := mongo.NewCondition()

src/source_controller/coreservice/core/association/kind.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (m *associationKind) CreateAssociationKind(ctx core.ContextParams, inputPar
3636
}
3737
if exists {
3838
blog.Errorf("association kind (%#v)is duplicated", inputParam.Data)
39-
return nil, ctx.Error.Error(common.CCErrCommDuplicateItem)
39+
return nil, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
4040
}
4141

4242
id, err := m.save(ctx, inputParam.Data)

src/source_controller/coreservice/core/association/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (m *associationModel) CreateModelAssociation(ctx core.ContextParams, inputP
4242
}
4343
if exists {
4444
blog.Warnf("request(%s): it is failed create a new association, because of the association ID (%s) is exists", ctx.ReqID, inputParam.Spec.AsstKindID)
45-
return &metadata.CreateOneDataResult{}, ctx.Error.Error(common.CCErrCommDuplicateItem)
45+
return &metadata.CreateOneDataResult{}, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
4646
}
4747

4848
exists, err = m.isExistsAssociationObjectWithAnotherObject(ctx, inputParam.Spec.ObjectID, inputParam.Spec.AsstObjID)

src/source_controller/coreservice/core/model/classification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (m *modelClassification) CreateOneModelClassification(ctx core.ContextParam
4343
}
4444
if exists {
4545
blog.Errorf("classification (%#v)is duplicated", inputParam.Data)
46-
return nil, ctx.Error.Error(common.CCErrCommDuplicateItem)
46+
return nil, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
4747
}
4848

4949
inputParam.Data.OwnerID = ctx.SupplierAccount

src/source_controller/coreservice/core/model/classification_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestCreateOneClassification(t *testing.T) {
4747
ClassificationName: "test_classification_name",
4848
},
4949
})
50-
require.EqualError(t, err, defaultCtx.Error.Error(common.CCErrCommDuplicateItem).Error())
50+
require.EqualError(t, err, defaultCtx.Error.Errorf(common.CCErrCommDuplicateItem, "").Error())
5151

5252
}
5353

src/source_controller/coreservice/core/model/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (m *modelManager) CreateModel(ctx core.ContextParams, inputParam metadata.C
9191

9292
if exists {
9393
blog.Warnf("request(%s): it is failed to create a new model , because of the model (%s) is already exists ", ctx.ReqID, inputParam.Spec.ObjectID)
94-
return dataResult, ctx.Error.Error(common.CCErrCommDuplicateItem)
94+
return dataResult, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
9595
}
9696
inputParam.Spec.OwnerID = ctx.SupplierAccount
9797
id, err := m.save(ctx, &inputParam.Spec)

src/source_controller/coreservice/core/model/unique_crud.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (m *modelAttrUnique) createModelAttrUnique(ctx core.ContextParams, objID st
7272
err := m.recheckUniqueForExistsInsts(ctx, objID, inputParam.Data.Keys, inputParam.Data.MustCheck)
7373
if nil != err {
7474
blog.Errorf("[CreateObjectUnique] recheckUniqueForExistsInsts for %s with %#v error: %#v", objID, inputParam, err)
75-
return 0, ctx.Error.Error(common.CCErrCommDuplicateItem)
75+
return 0, ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
7676
}
7777

7878
id, err := m.dbProxy.NextSequence(ctx, common.BKTableNameObjUnique)
@@ -136,7 +136,7 @@ func (m *modelAttrUnique) updateModelAttrUnique(ctx core.ContextParams, objID st
136136
err := m.recheckUniqueForExistsInsts(ctx, objID, unique.Keys, unique.MustCheck)
137137
if nil != err {
138138
blog.Errorf("[UpdateObjectUnique] recheckUniqueForExistsInsts for %s with %#v error: %#v", objID, unique, err)
139-
return ctx.Error.Error(common.CCErrCommDuplicateItem)
139+
return ctx.Error.Errorf(common.CCErrCommDuplicateItem, "")
140140
}
141141

142142
cond := condition.CreateCondition()

src/source_controller/hostcontroller/service/favourite.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (s *Service) UpdateHostFavouriteByID(req *restful.Request, resp *restful.Re
126126
}
127127
if 0 < rowCount {
128128
blog.Errorf("host user api name duplicate , params:%v", dupParams)
129-
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Error(common.CCErrCommDuplicateItem)})
129+
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Errorf(common.CCErrCommDuplicateItem, "")})
130130
return
131131
}
132132
}

src/source_controller/hostcontroller/service/user.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (s *Service) AddUserConfig(req *restful.Request, resp *restful.Response) {
6969
}
7070
if 0 != rowCount {
7171
blog.Errorf("add user config, [%s] user api is exist", addQuery.Name)
72-
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Error(common.CCErrCommDuplicateItem)})
72+
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Errorf(common.CCErrCommDuplicateItem, "")})
7373
return
7474
}
7575

@@ -145,7 +145,7 @@ func (s *Service) UpdateUserConfig(req *restful.Request, resp *restful.Response)
145145
}
146146
if 0 < rowCount {
147147
blog.V(5).Infof("host user api name duplicate , params:%v", dupParams)
148-
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Error(common.CCErrCommDuplicateItem)})
148+
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.Errorf(common.CCErrCommDuplicateItem, "")})
149149
return
150150
}
151151
}

0 commit comments

Comments
 (0)