From 450784dfe99a27762496ae0da773459ceda80ce4 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Mon, 15 Aug 2022 11:10:36 +0800 Subject: [PATCH] #2624 add response error --- routers/admin/resources.go | 4 ++-- routers/response/error.go | 14 +++++++++++ routers/response/response.go | 4 ++++ routers/response/response_list.go | 4 ++++ .../resource/resource_specification.go | 24 +++++++++---------- 5 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 routers/response/error.go create mode 100644 routers/response/response_list.go diff --git a/routers/admin/resources.go b/routers/admin/resources.go index 0c81bc3dd..2286dfff2 100644 --- a/routers/admin/resources.go +++ b/routers/admin/resources.go @@ -148,7 +148,7 @@ func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecif id := ctx.ParamsInt64(":id") action := ctx.Query("action") - var err error + var err *response.BizError switch action { case "edit": if req.UnitPrice < 0 { @@ -165,7 +165,7 @@ func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecif if err != nil { log.Error("UpdateResourceSpecification error. %v", err) - ctx.JSON(http.StatusOK, response.ServerError(err.Error())) + ctx.JSON(http.StatusOK, response.ResponseError(err)) return } ctx.JSON(http.StatusOK, response.Success()) diff --git a/routers/response/error.go b/routers/response/error.go new file mode 100644 index 000000000..685267da9 --- /dev/null +++ b/routers/response/error.go @@ -0,0 +1,14 @@ +package response + +type BizError struct { + Code int + Err string +} + +func (b BizError) Error() string { + return b.Err +} + +func NewBizError(err error) *BizError { + return &BizError{Code: RESPONSE_CODE_ERROR_DEFAULT, Err: err.Error()} +} diff --git a/routers/response/response.go b/routers/response/response.go index e87471d4c..ccd6be445 100644 --- a/routers/response/response.go +++ b/routers/response/response.go @@ -24,6 +24,10 @@ func ServerError(msg string) *AiforgeResponse { return &AiforgeResponse{Code: RESPONSE_CODE_ERROR_DEFAULT, Msg: msg} } +func ResponseError(err *BizError) *AiforgeResponse { + return &AiforgeResponse{Code: err.Code, Msg: err.Err} +} + func SuccessWithData(data interface{}) *AiforgeResponse { return &AiforgeResponse{Code: RESPONSE_CODE_SUCCESS, Msg: RESPONSE_MSG_SUCCESS, Data: data} } diff --git a/routers/response/response_list.go b/routers/response/response_list.go new file mode 100644 index 000000000..5e057bfd0 --- /dev/null +++ b/routers/response/response_list.go @@ -0,0 +1,4 @@ +package response + +var RESOURCE_QUEUE_NOT_AVAILABLE = &BizError{Code: 1001, Err: "resource queue not available"} +var SPECIFICATION_NOT_EXIST = &BizError{Code: 1002, Err: "specification not exist"} diff --git a/services/cloudbrain/resource/resource_specification.go b/services/cloudbrain/resource/resource_specification.go index 0093ab4cd..e597e2656 100644 --- a/services/cloudbrain/resource/resource_specification.go +++ b/services/cloudbrain/resource/resource_specification.go @@ -4,9 +4,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/grampus" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/routers/response" "code.gitea.io/gitea/services/admin/operate_log" "encoding/json" - "errors" "fmt" "strings" ) @@ -22,19 +22,19 @@ func AddResourceSpecification(doerId int64, req models.ResourceSpecificationReq) return nil } -func UpdateResourceSpecification(doerId int64, specId int64, req models.ResourceSpecificationReq) error { +func UpdateResourceSpecification(doerId int64, specId int64, req models.ResourceSpecificationReq) *response.BizError { oldSpec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: specId}) if err != nil { - return err + return response.NewBizError(err) } if oldSpec == nil { - return errors.New("specification not exist") + return response.SPECIFICATION_NOT_EXIST } n, err := models.UpdateResourceSpecificationById(specId, models.ResourceSpecification{ UnitPrice: req.UnitPrice, }) if err != nil { - return err + return response.NewBizError(err) } if n > 0 { @@ -125,23 +125,23 @@ func GetResourceSpecificationList(opts models.SearchResourceSpecificationOptions return models.NewResourceSpecAndQueueListRes(n, r), nil } -func ResourceSpecOnShelf(doerId int64, id int64, req models.ResourceSpecificationReq) error { +func ResourceSpecOnShelf(doerId int64, id int64, req models.ResourceSpecificationReq) *response.BizError { spec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: id}) if err != nil { - return err + return response.NewBizError(err) } if spec == nil { - return errors.New("specification not exist") + return response.SPECIFICATION_NOT_EXIST } if q, err := models.GetResourceQueue(&models.ResourceQueue{ID: spec.QueueId}); err != nil || q == nil { - return errors.New("resource queue not available") + return response.RESOURCE_QUEUE_NOT_AVAILABLE } n, err := models.ResourceSpecOnShelf(id, models.ResourceSpecification{ UnitPrice: req.UnitPrice, }) if err != nil { - return err + return response.NewBizError(err) } if n > 0 { newSpec, _ := models.GetResourceSpecification(&models.ResourceSpecification{ID: id}) @@ -150,10 +150,10 @@ func ResourceSpecOnShelf(doerId int64, id int64, req models.ResourceSpecificatio return nil } -func ResourceSpecOffShelf(doerId int64, id int64) error { +func ResourceSpecOffShelf(doerId int64, id int64) *response.BizError { _, err := models.ResourceSpecOffShelf(id) if err != nil { - return err + return response.NewBizError(err) } return nil