feat: 完善api错误提示
This commit is contained in:
@@ -3,9 +3,9 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
@@ -26,26 +26,26 @@ func isValidGroupName(name string) bool {
|
||||
func (s *Server) CreateGroup(c *gin.Context) {
|
||||
var group models.Group
|
||||
if err := c.ShouldBindJSON(&group); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// Validation
|
||||
if !isValidGroupName(group.Name) {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group name format. Use lowercase letters and underscores, and do not start with an underscore.")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid group name format. Use 3-30 lowercase letters, numbers, and underscores."))
|
||||
return
|
||||
}
|
||||
if len(group.Upstreams) == 0 {
|
||||
response.Error(c, http.StatusBadRequest, "At least one upstream is required")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "At least one upstream is required"))
|
||||
return
|
||||
}
|
||||
if group.ChannelType == "" {
|
||||
response.Error(c, http.StatusBadRequest, "Channel type is required")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Channel type is required"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.DB.Create(&group).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to create group")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (s *Server) CreateGroup(c *gin.Context) {
|
||||
func (s *Server) ListGroups(c *gin.Context) {
|
||||
var groups []models.Group
|
||||
if err := s.DB.Order("sort asc, id desc").Find(&groups).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to list groups")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
response.Success(c, groups)
|
||||
@@ -66,32 +66,32 @@ func (s *Server) ListGroups(c *gin.Context) {
|
||||
func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
var group models.Group
|
||||
if err := s.DB.First(&group, id).Error; err != nil {
|
||||
response.Error(c, http.StatusNotFound, "Group not found")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
var updateData models.Group
|
||||
if err := c.ShouldBindJSON(&updateData); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// Validate group name if it's being updated
|
||||
if updateData.Name != "" && !isValidGroupName(updateData.Name) {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group name format. Use lowercase letters and underscores, and do not start with an underscore.")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid group name format. Use 3-30 lowercase letters, numbers, and underscores."))
|
||||
return
|
||||
}
|
||||
|
||||
// Use a transaction to ensure atomicity
|
||||
tx := s.DB.Begin()
|
||||
if tx.Error != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to start transaction")
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
var updateMap map[string]interface{}
|
||||
updateBytes, _ := json.Marshal(updateData)
|
||||
if err := json.Unmarshal(updateBytes, &updateMap); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Failed to process update data")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Failed to process update data"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
if configMap, isMap := config.(map[string]interface{}); isMap {
|
||||
configJSON, err := json.Marshal(configMap)
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Failed to process config data")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Failed to process config data"))
|
||||
return
|
||||
}
|
||||
updateMap["config"] = string(configJSON)
|
||||
@@ -120,7 +120,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
if upstreamsSlice, isSlice := upstreams.([]interface{}); isSlice {
|
||||
upstreamsJSON, err := json.Marshal(upstreamsSlice)
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Failed to process upstreams data")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Failed to process upstreams data"))
|
||||
return
|
||||
}
|
||||
updateMap["upstreams"] = string(upstreamsJSON)
|
||||
@@ -136,20 +136,20 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
// Use Updates with a map to only update provided fields, including zero values
|
||||
if err := tx.Model(&group).Updates(updateMap).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to update group")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to commit transaction")
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
// Re-fetch the group to return the updated data
|
||||
var updatedGroup models.Group
|
||||
if err := s.DB.First(&updatedGroup, id).Error; err != nil {
|
||||
response.Error(c, http.StatusNotFound, "Failed to fetch updated group data")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -160,14 +160,14 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
||||
func (s *Server) DeleteGroup(c *gin.Context) {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
// Use a transaction to ensure atomicity
|
||||
tx := s.DB.Begin()
|
||||
if tx.Error != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to start transaction")
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
@@ -179,23 +179,23 @@ func (s *Server) DeleteGroup(c *gin.Context) {
|
||||
// Also delete associated API keys
|
||||
if err := tx.Where("group_id = ?", id).Delete(&models.APIKey{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to delete associated API keys")
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
if result := tx.Delete(&models.Group{}, id); result.Error != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to delete group")
|
||||
response.Error(c, app_errors.ParseDBError(result.Error))
|
||||
return
|
||||
} else if result.RowsAffected == 0 {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusNotFound, "Group not found")
|
||||
response.Error(c, app_errors.ErrResourceNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to commit transaction")
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -2,9 +2,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -18,13 +18,13 @@ type CreateKeysRequest struct {
|
||||
func (s *Server) CreateKeysInGroup(c *gin.Context) {
|
||||
groupID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
var req CreateKeysRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func (s *Server) CreateKeysInGroup(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := s.DB.Create(&newKeys).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to create keys")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,13 +49,13 @@ func (s *Server) CreateKeysInGroup(c *gin.Context) {
|
||||
func (s *Server) ListKeysInGroup(c *gin.Context) {
|
||||
groupID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
var keys []models.APIKey
|
||||
if err := s.DB.Where("group_id = ?", groupID).Find(&keys).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to list keys")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -66,19 +66,19 @@ func (s *Server) ListKeysInGroup(c *gin.Context) {
|
||||
func (s *Server) UpdateKey(c *gin.Context) {
|
||||
groupID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
keyID, err := strconv.Atoi(c.Param("key_id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid key ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid key ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
var key models.APIKey
|
||||
if err := s.DB.Where("group_id = ? AND id = ?", groupID, keyID).First(&key).Error; err != nil {
|
||||
response.Error(c, http.StatusNotFound, "Key not found in this group")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -86,13 +86,13 @@ func (s *Server) UpdateKey(c *gin.Context) {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&updateData); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
key.Status = updateData.Status
|
||||
if err := s.DB.Save(&key).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to update key")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -107,18 +107,18 @@ type DeleteKeysRequest struct {
|
||||
func (s *Server) DeleteKeys(c *gin.Context) {
|
||||
groupID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid group ID")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Invalid group ID format"))
|
||||
return
|
||||
}
|
||||
|
||||
var req DeleteKeysRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.KeyIDs) == 0 {
|
||||
response.Error(c, http.StatusBadRequest, "No key IDs provided")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "No key IDs provided"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -129,23 +129,23 @@ func (s *Server) DeleteKeys(c *gin.Context) {
|
||||
var count int64
|
||||
if err := tx.Model(&models.APIKey{}).Where("id IN ? AND group_id = ?", req.KeyIDs, groupID).Count(&count).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to verify keys")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
if count != int64(len(req.KeyIDs)) {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusForbidden, "One or more keys do not belong to the specified group")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrForbidden, "One or more keys do not belong to the specified group"))
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the keys
|
||||
if err := tx.Where("id IN ?", req.KeyIDs).Delete(&models.APIKey{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to delete keys")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
response.Success(c, gin.H{"message": "Keys deleted successfully"})
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gpt-load/internal/db"
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
)
|
||||
@@ -66,7 +67,7 @@ func GetLogs(c *gin.Context) {
|
||||
query.Count(&total)
|
||||
err := query.Order("timestamp desc").Offset(offset).Limit(size).Find(&logs).Error
|
||||
if err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to get logs")
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -76,4 +77,4 @@ func GetLogs(c *gin.Context) {
|
||||
"size": size,
|
||||
"data": logs,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"gpt-load/internal/config"
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
|
||||
@@ -40,7 +41,7 @@ func GetSettings(c *gin.Context) {
|
||||
func UpdateSettings(c *gin.Context) {
|
||||
var settingsMap map[string]any
|
||||
if err := c.ShouldBindJSON(&settingsMap); err != nil {
|
||||
response.BadRequest(c, "Invalid request body")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -53,14 +54,14 @@ func UpdateSettings(c *gin.Context) {
|
||||
|
||||
// 更新配置
|
||||
if err := settingsManager.UpdateSettings(settingsMap); err != nil {
|
||||
response.InternalError(c, "Failed to update settings: "+err.Error())
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrDatabase, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 重载系统配置
|
||||
if err := settingsManager.LoadFromDatabase(); err != nil {
|
||||
logrus.Errorf("Failed to reload system settings: %v", err)
|
||||
response.InternalError(c, "Failed to reload system settings")
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, "Failed to reload system settings after update"))
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user