throw exception
This commit is contained in:
parent
7d5d6f47cd
commit
fae1868bd5
104
client/api/ai.go
104
client/api/ai.go
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
@ -43,7 +44,7 @@ func (a *aiApi) TrainAlgorithmsHandler(c *gin.Context) {
|
|||
}
|
||||
algorithms, err := a.service.TrainAlgorithms(c, pfId)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get train algorithms", nil)
|
||||
model.Response(c, http.StatusBadRequest, "failed to get train algorithms", err.Error())
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", algorithms)
|
||||
|
@ -53,7 +54,7 @@ func (a *aiApi) TrainAlgorithmsHandler(c *gin.Context) {
|
|||
func (a *aiApi) AllTrainAlgorithmsHandler(c *gin.Context) {
|
||||
algorithms, err := a.service.AllTrainAlgorithms(c)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get all train algorithms", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get all train algorithms", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", algorithms)
|
||||
|
@ -70,23 +71,19 @@ func (a *aiApi) CreateAlgorithmHandler(c *gin.Context) {
|
|||
|
||||
var param algorithm.CreateParam
|
||||
if err := c.ShouldBindJSON(¶m); err != nil {
|
||||
if ve, ok := err.(validator.ValidationErrors); ok {
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
var errorMsg []string
|
||||
for _, e := range ve {
|
||||
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
|
||||
}
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
|
||||
} else {
|
||||
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
|
||||
}
|
||||
return
|
||||
//model.Response(c, http.StatusBadRequest, "invalid request body", nil)
|
||||
//return
|
||||
}
|
||||
|
||||
resp, err := a.service.CreateAlgorithm(c.Request.Context(), pfId, ¶m)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -104,21 +101,20 @@ func (a *aiApi) CreateDatasetHandler(c *gin.Context) {
|
|||
|
||||
var param dataset.CreateParam
|
||||
if err := c.ShouldBindJSON(¶m); err != nil {
|
||||
if ve, ok := err.(validator.ValidationErrors); ok {
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
var errorMsg []string
|
||||
for _, e := range ve {
|
||||
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
|
||||
}
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
|
||||
} else {
|
||||
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := a.service.CreateDataset(c.Request.Context(), pfId, ¶m)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -130,27 +126,26 @@ func (a *aiApi) CreateModelHandler(c *gin.Context) {
|
|||
pfIdStr := c.Query("pfId")
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
|
||||
var param aiModel.CreateParam
|
||||
if err := c.ShouldBindJSON(¶m); err != nil {
|
||||
if ve, ok := err.(validator.ValidationErrors); ok {
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
var errorMsg []string
|
||||
for _, e := range ve {
|
||||
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
|
||||
}
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
|
||||
} else {
|
||||
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
|
||||
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := a.service.CreateModel(c.Request.Context(), pfId, ¶m)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -163,11 +158,16 @@ func (a *aiApi) GetResourceSpecsHandler(c *gin.Context) {
|
|||
rtype := c.Query("rType")
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
a.service.GetResourceSpecs(c, pfId, rtype)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
_, err = a.service.GetResourceSpecs(c, pfId, rtype)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", err)
|
||||
}
|
||||
|
||||
// TrainResourcesHandler 处理获取特定平台训练资源的请求
|
||||
|
@ -175,12 +175,12 @@ func (a *aiApi) TrainResourcesHandler(c *gin.Context) {
|
|||
pfIdStr := c.Query("pfId")
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
resources, err := a.service.TrainResources(c, pfId)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get train resources", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get train resources", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", resources)
|
||||
|
@ -190,7 +190,7 @@ func (a *aiApi) TrainResourcesHandler(c *gin.Context) {
|
|||
func (a *aiApi) AllTrainResourcesHandler(c *gin.Context) {
|
||||
resources, err := a.service.AllTrainResources(c)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get all train resources", nil)
|
||||
model.Response(c, http.StatusInternalServerError, "failed to get all train resources", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", resources)
|
||||
|
@ -200,7 +200,7 @@ func (a *aiApi) AllTrainResourcesHandler(c *gin.Context) {
|
|||
func (a *aiApi) CreateTrainTaskHandler(c *gin.Context) {
|
||||
var param service.CreateTrainTaskParam
|
||||
if err := c.ShouldBindJSON(¶m); err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid request body", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid request body", err)
|
||||
return
|
||||
}
|
||||
resp, err := a.service.CreateTrainTask(c, ¶m)
|
||||
|
@ -216,16 +216,20 @@ func (a *aiApi) TaskResultSyncHandler(c *gin.Context) {
|
|||
pfIdStr := c.Query("pfId")
|
||||
var param task.ResultSyncParam
|
||||
if err := c.ShouldBindJSON(¶m); err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid request body", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid request body", err)
|
||||
return
|
||||
}
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
a.service.TaskResultSync(c, pfId, ¶m)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
err = a.service.TaskResultSync(c, pfId, ¶m)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "task Sync failed", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", err)
|
||||
}
|
||||
|
||||
// TaskLogHandler 查询任务日志
|
||||
|
@ -235,11 +239,15 @@ func (a *aiApi) TaskLogHandler(c *gin.Context) {
|
|||
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
a.service.TaskLog(c, pfId, taskId)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
_, err = a.service.TaskLog(c, pfId, taskId)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "get task log failed", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", err)
|
||||
}
|
||||
|
||||
// TrainTaskDetailHandler 查询训练任务详情
|
||||
|
@ -249,11 +257,15 @@ func (a *aiApi) TrainTaskDetailHandler(c *gin.Context) {
|
|||
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
a.service.GetTrainingTask(c, pfId, taskId)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
_, err = a.service.GetTrainingTask(c, pfId, taskId)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "get task detail failed", err)
|
||||
return
|
||||
}
|
||||
model.Response(c, http.StatusOK, "success", err)
|
||||
}
|
||||
|
||||
// InferTaskDetailHandler 查询推理任务详情
|
||||
|
@ -263,21 +275,13 @@ func (a *aiApi) InferTaskDetailHandler(c *gin.Context) {
|
|||
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
|
||||
return
|
||||
}
|
||||
a.service.GetInferenceTask(c, pfId, taskId)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
}
|
||||
|
||||
// TestFuncResHandler 处理测试资源相关功能的请求
|
||||
func (a *aiApi) TestFuncResHandler(c *gin.Context) {
|
||||
pfIdStr := c.Query("pfId")
|
||||
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
|
||||
_, err = a.service.GetInferenceTask(c, pfId, taskId)
|
||||
if err != nil {
|
||||
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
|
||||
model.Response(c, http.StatusBadRequest, "get infer task detail failed", err)
|
||||
return
|
||||
}
|
||||
a.service.TestFuncRes(c, pfId)
|
||||
model.Response(c, http.StatusOK, "success", nil)
|
||||
model.Response(c, http.StatusOK, "success", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue