Browse Source

#2624

add response error
tags/v1.22.8.2^2
chenyifan01 4 years ago
parent
commit
450784dfe9
5 changed files with 36 additions and 14 deletions
  1. +2
    -2
      routers/admin/resources.go
  2. +14
    -0
      routers/response/error.go
  3. +4
    -0
      routers/response/response.go
  4. +4
    -0
      routers/response/response_list.go
  5. +12
    -12
      services/cloudbrain/resource/resource_specification.go

+ 2
- 2
routers/admin/resources.go View File

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


+ 14
- 0
routers/response/error.go View File

@@ -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()}
}

+ 4
- 0
routers/response/response.go View File

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


+ 4
- 0
routers/response/response_list.go View File

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

+ 12
- 12
services/cloudbrain/resource/resource_specification.go View File

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



Loading…
Cancel
Save