feat: 完善api错误提示

This commit is contained in:
tbphp
2025-07-03 21:33:16 +08:00
parent 745c646530
commit c0594d068e
10 changed files with 127 additions and 220 deletions

View File

@@ -176,7 +176,7 @@ func (m *Manager) Validate() error {
for _, err := range validationErrors { for _, err := range validationErrors {
logrus.Errorf(" - %s", err) logrus.Errorf(" - %s", err)
} }
return errors.NewAppErrorWithDetails(errors.ErrConfigValidation, "Configuration validation failed", strings.Join(validationErrors, "; ")) return errors.NewAPIError(errors.ErrValidation, strings.Join(validationErrors, "; "))
} }
return nil return nil

View File

@@ -1,129 +1,67 @@
// Package errors defines custom error types for the application
package errors package errors
import ( import (
"fmt" "errors"
"net/http" "net/http"
"github.com/go-sql-driver/mysql"
"gorm.io/gorm"
) )
// ErrorCode represents different types of errors // APIError defines a standard error structure for API responses.
type ErrorCode int type APIError struct {
HTTPStatus int
const ( Code string
// Configuration errors Message string
ErrConfigInvalid ErrorCode = iota + 1000
ErrConfigMissing
ErrConfigValidation
// Key management errors
ErrNoKeysAvailable ErrorCode = iota + 2000
ErrKeyFileNotFound
ErrKeyFileInvalid
ErrAllKeysBlacklisted
// Proxy errors
ErrProxyRequest ErrorCode = iota + 3000
ErrProxyResponse
ErrProxyTimeout
ErrProxyRetryExhausted
// Authentication errors
ErrAuthInvalid ErrorCode = iota + 4000
ErrAuthMissing
ErrAuthExpired
// Server errors
ErrServerInternal ErrorCode = iota + 5000
ErrServerUnavailable
)
// AppError represents a custom application error
type AppError struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
HTTPStatus int `json:"-"`
Cause error `json:"-"`
} }
// Error implements the error interface // Error implements the error interface.
func (e *AppError) Error() string { func (e *APIError) Error() string {
if e.Details != "" { return e.Message
return fmt.Sprintf("[%d] %s: %s", e.Code, e.Message, e.Details)
}
return fmt.Sprintf("[%d] %s", e.Code, e.Message)
} }
// Unwrap returns the underlying error // Predefined API errors
func (e *AppError) Unwrap() error {
return e.Cause
}
// NewAppError creates a new application error
func NewAppError(code ErrorCode, message string) *AppError {
return &AppError{
Code: code,
Message: message,
HTTPStatus: getHTTPStatusForCode(code),
}
}
// NewAppErrorWithDetails creates a new application error with details
func NewAppErrorWithDetails(code ErrorCode, message, details string) *AppError {
return &AppError{
Code: code,
Message: message,
Details: details,
HTTPStatus: getHTTPStatusForCode(code),
}
}
// NewAppErrorWithCause creates a new application error with underlying cause
func NewAppErrorWithCause(code ErrorCode, message string, cause error) *AppError {
return &AppError{
Code: code,
Message: message,
HTTPStatus: getHTTPStatusForCode(code),
Cause: cause,
}
}
// getHTTPStatusForCode maps error codes to HTTP status codes
func getHTTPStatusForCode(code ErrorCode) int {
switch {
case code >= 1000 && code < 2000: // Configuration errors
return http.StatusInternalServerError
case code >= 2000 && code < 3000: // Key management errors
return http.StatusServiceUnavailable
case code >= 3000 && code < 4000: // Proxy errors
return http.StatusBadGateway
case code >= 4000 && code < 5000: // Authentication errors
return http.StatusUnauthorized
case code >= 5000 && code < 6000: // Server errors
return http.StatusInternalServerError
default:
return http.StatusInternalServerError
}
}
// IsRetryable determines if an error is retryable
func IsRetryable(err error) bool {
if appErr, ok := err.(*AppError); ok {
switch appErr.Code {
case ErrProxyTimeout, ErrServerUnavailable:
return true
default:
return false
}
}
return false
}
// Common error instances
var ( var (
ErrNoAPIKeysAvailable = NewAppError(ErrNoKeysAvailable, "No API keys available") ErrBadRequest = &APIError{HTTPStatus: http.StatusBadRequest, Code: "BAD_REQUEST", Message: "Invalid request parameters"}
ErrAllAPIKeysBlacklisted = NewAppError(ErrAllKeysBlacklisted, "All API keys are blacklisted") ErrInvalidJSON = &APIError{HTTPStatus: http.StatusBadRequest, Code: "INVALID_JSON", Message: "Invalid JSON format"}
ErrInvalidConfiguration = NewAppError(ErrConfigInvalid, "Invalid configuration") ErrValidation = &APIError{HTTPStatus: http.StatusBadRequest, Code: "VALIDATION_FAILED", Message: "Input validation failed"}
ErrAuthenticationRequired = NewAppError(ErrAuthMissing, "Authentication required") ErrDuplicateResource = &APIError{HTTPStatus: http.StatusConflict, Code: "DUPLICATE_RESOURCE", Message: "Resource already exists"}
ErrInvalidAuthToken = NewAppError(ErrAuthInvalid, "Invalid authentication token") ErrResourceNotFound = &APIError{HTTPStatus: http.StatusNotFound, Code: "NOT_FOUND", Message: "Resource not found"}
ErrInternalServer = &APIError{HTTPStatus: http.StatusInternalServerError, Code: "INTERNAL_SERVER_ERROR", Message: "An unexpected error occurred"}
ErrDatabase = &APIError{HTTPStatus: http.StatusInternalServerError, Code: "DATABASE_ERROR", Message: "Database operation failed"}
ErrUnauthorized = &APIError{HTTPStatus: http.StatusUnauthorized, Code: "UNAUTHORIZED", Message: "Authentication failed"}
ErrForbidden = &APIError{HTTPStatus: http.StatusForbidden, Code: "FORBIDDEN", Message: "You do not have permission to access this resource"}
) )
// NewAPIError creates a new APIError with a custom message.
func NewAPIError(base *APIError, message string) *APIError {
return &APIError{
HTTPStatus: base.HTTPStatus,
Code: base.Code,
Message: message,
}
}
// ParseDBError intelligently converts a GORM error into a standard APIError.
func ParseDBError(err error) *APIError {
if err == nil {
return nil
}
// Handle record not found error
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrResourceNotFound
}
// Handle MySQL specific errors
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) {
switch mysqlErr.Number {
case 1062: // Duplicate entry for unique key
return ErrDuplicateResource
}
}
// Default to a generic database error
return ErrDatabase
}

View File

@@ -3,9 +3,9 @@ package handler
import ( import (
"encoding/json" "encoding/json"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/models" "gpt-load/internal/models"
"gpt-load/internal/response" "gpt-load/internal/response"
"net/http"
"regexp" "regexp"
"strconv" "strconv"
@@ -26,26 +26,26 @@ func isValidGroupName(name string) bool {
func (s *Server) CreateGroup(c *gin.Context) { func (s *Server) CreateGroup(c *gin.Context) {
var group models.Group var group models.Group
if err := c.ShouldBindJSON(&group); err != nil { 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 return
} }
// Validation // Validation
if !isValidGroupName(group.Name) { 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 return
} }
if len(group.Upstreams) == 0 { 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 return
} }
if group.ChannelType == "" { 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 return
} }
if err := s.DB.Create(&group).Error; err != nil { 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 return
} }
@@ -56,7 +56,7 @@ func (s *Server) CreateGroup(c *gin.Context) {
func (s *Server) ListGroups(c *gin.Context) { func (s *Server) ListGroups(c *gin.Context) {
var groups []models.Group var groups []models.Group
if err := s.DB.Order("sort asc, id desc").Find(&groups).Error; err != nil { 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 return
} }
response.Success(c, groups) response.Success(c, groups)
@@ -66,32 +66,32 @@ func (s *Server) ListGroups(c *gin.Context) {
func (s *Server) UpdateGroup(c *gin.Context) { func (s *Server) UpdateGroup(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id")) id, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
var group models.Group var group models.Group
if err := s.DB.First(&group, id).Error; err != nil { 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 return
} }
var updateData models.Group var updateData models.Group
if err := c.ShouldBindJSON(&updateData); err != nil { 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 return
} }
// Validate group name if it's being updated // Validate group name if it's being updated
if updateData.Name != "" && !isValidGroupName(updateData.Name) { 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 return
} }
// Use a transaction to ensure atomicity // Use a transaction to ensure atomicity
tx := s.DB.Begin() tx := s.DB.Begin()
if tx.Error != nil { if tx.Error != nil {
response.Error(c, http.StatusInternalServerError, "Failed to start transaction") response.Error(c, app_errors.ErrDatabase)
return return
} }
@@ -99,7 +99,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
var updateMap map[string]interface{} var updateMap map[string]interface{}
updateBytes, _ := json.Marshal(updateData) updateBytes, _ := json.Marshal(updateData)
if err := json.Unmarshal(updateBytes, &updateMap); err != nil { 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 return
} }
@@ -108,7 +108,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
if configMap, isMap := config.(map[string]interface{}); isMap { if configMap, isMap := config.(map[string]interface{}); isMap {
configJSON, err := json.Marshal(configMap) configJSON, err := json.Marshal(configMap)
if err != nil { 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 return
} }
updateMap["config"] = string(configJSON) updateMap["config"] = string(configJSON)
@@ -120,7 +120,7 @@ func (s *Server) UpdateGroup(c *gin.Context) {
if upstreamsSlice, isSlice := upstreams.([]interface{}); isSlice { if upstreamsSlice, isSlice := upstreams.([]interface{}); isSlice {
upstreamsJSON, err := json.Marshal(upstreamsSlice) upstreamsJSON, err := json.Marshal(upstreamsSlice)
if err != nil { 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 return
} }
updateMap["upstreams"] = string(upstreamsJSON) 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 // Use Updates with a map to only update provided fields, including zero values
if err := tx.Model(&group).Updates(updateMap).Error; err != nil { if err := tx.Model(&group).Updates(updateMap).Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to update group") response.Error(c, app_errors.ParseDBError(err))
return return
} }
if err := tx.Commit().Error; err != nil { if err := tx.Commit().Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to commit transaction") response.Error(c, app_errors.ErrDatabase)
return return
} }
// Re-fetch the group to return the updated data // Re-fetch the group to return the updated data
var updatedGroup models.Group var updatedGroup models.Group
if err := s.DB.First(&updatedGroup, id).Error; err != nil { 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 return
} }
@@ -160,14 +160,14 @@ func (s *Server) UpdateGroup(c *gin.Context) {
func (s *Server) DeleteGroup(c *gin.Context) { func (s *Server) DeleteGroup(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id")) id, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
// Use a transaction to ensure atomicity // Use a transaction to ensure atomicity
tx := s.DB.Begin() tx := s.DB.Begin()
if tx.Error != nil { if tx.Error != nil {
response.Error(c, http.StatusInternalServerError, "Failed to start transaction") response.Error(c, app_errors.ErrDatabase)
return return
} }
defer func() { defer func() {
@@ -179,23 +179,23 @@ func (s *Server) DeleteGroup(c *gin.Context) {
// Also delete associated API keys // Also delete associated API keys
if err := tx.Where("group_id = ?", id).Delete(&models.APIKey{}).Error; err != nil { if err := tx.Where("group_id = ?", id).Delete(&models.APIKey{}).Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to delete associated API keys") response.Error(c, app_errors.ErrDatabase)
return return
} }
if result := tx.Delete(&models.Group{}, id); result.Error != nil { if result := tx.Delete(&models.Group{}, id); result.Error != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to delete group") response.Error(c, app_errors.ParseDBError(result.Error))
return return
} else if result.RowsAffected == 0 { } else if result.RowsAffected == 0 {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusNotFound, "Group not found") response.Error(c, app_errors.ErrResourceNotFound)
return return
} }
if err := tx.Commit().Error; err != nil { if err := tx.Commit().Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to commit transaction") response.Error(c, app_errors.ErrDatabase)
return return
} }

View File

@@ -2,9 +2,9 @@
package handler package handler
import ( import (
app_errors "gpt-load/internal/errors"
"gpt-load/internal/models" "gpt-load/internal/models"
"gpt-load/internal/response" "gpt-load/internal/response"
"net/http"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -18,13 +18,13 @@ type CreateKeysRequest struct {
func (s *Server) CreateKeysInGroup(c *gin.Context) { func (s *Server) CreateKeysInGroup(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id")) groupID, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
var req CreateKeysRequest var req CreateKeysRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
@@ -38,7 +38,7 @@ func (s *Server) CreateKeysInGroup(c *gin.Context) {
} }
if err := s.DB.Create(&newKeys).Error; err != nil { 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 return
} }
@@ -49,13 +49,13 @@ func (s *Server) CreateKeysInGroup(c *gin.Context) {
func (s *Server) ListKeysInGroup(c *gin.Context) { func (s *Server) ListKeysInGroup(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id")) groupID, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
var keys []models.APIKey var keys []models.APIKey
if err := s.DB.Where("group_id = ?", groupID).Find(&keys).Error; err != nil { 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 return
} }
@@ -66,19 +66,19 @@ func (s *Server) ListKeysInGroup(c *gin.Context) {
func (s *Server) UpdateKey(c *gin.Context) { func (s *Server) UpdateKey(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id")) groupID, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
keyID, err := strconv.Atoi(c.Param("key_id")) keyID, err := strconv.Atoi(c.Param("key_id"))
if err != nil { 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 return
} }
var key models.APIKey var key models.APIKey
if err := s.DB.Where("group_id = ? AND id = ?", groupID, keyID).First(&key).Error; err != nil { 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 return
} }
@@ -86,13 +86,13 @@ func (s *Server) UpdateKey(c *gin.Context) {
Status string `json:"status"` Status string `json:"status"`
} }
if err := c.ShouldBindJSON(&updateData); err != nil { 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 return
} }
key.Status = updateData.Status key.Status = updateData.Status
if err := s.DB.Save(&key).Error; err != nil { 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 return
} }
@@ -107,18 +107,18 @@ type DeleteKeysRequest struct {
func (s *Server) DeleteKeys(c *gin.Context) { func (s *Server) DeleteKeys(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id")) groupID, err := strconv.Atoi(c.Param("id"))
if err != nil { 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 return
} }
var req DeleteKeysRequest var req DeleteKeysRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if len(req.KeyIDs) == 0 { 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 return
} }
@@ -129,20 +129,20 @@ func (s *Server) DeleteKeys(c *gin.Context) {
var count int64 var count int64
if err := tx.Model(&models.APIKey{}).Where("id IN ? AND group_id = ?", req.KeyIDs, groupID).Count(&count).Error; err != nil { if err := tx.Model(&models.APIKey{}).Where("id IN ? AND group_id = ?", req.KeyIDs, groupID).Count(&count).Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to verify keys") response.Error(c, app_errors.ParseDBError(err))
return return
} }
if count != int64(len(req.KeyIDs)) { if count != int64(len(req.KeyIDs)) {
tx.Rollback() 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 return
} }
// Delete the keys // Delete the keys
if err := tx.Where("id IN ?", req.KeyIDs).Delete(&models.APIKey{}).Error; err != nil { if err := tx.Where("id IN ?", req.KeyIDs).Delete(&models.APIKey{}).Error; err != nil {
tx.Rollback() tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to delete keys") response.Error(c, app_errors.ParseDBError(err))
return return
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gpt-load/internal/db" "gpt-load/internal/db"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/models" "gpt-load/internal/models"
"gpt-load/internal/response" "gpt-load/internal/response"
) )
@@ -66,7 +67,7 @@ func GetLogs(c *gin.Context) {
query.Count(&total) query.Count(&total)
err := query.Order("timestamp desc").Offset(offset).Limit(size).Find(&logs).Error err := query.Order("timestamp desc").Offset(offset).Limit(size).Find(&logs).Error
if err != nil { if err != nil {
response.Error(c, http.StatusInternalServerError, "Failed to get logs") response.Error(c, app_errors.ParseDBError(err))
return return
} }

View File

@@ -2,6 +2,7 @@ package handler
import ( import (
"gpt-load/internal/config" "gpt-load/internal/config"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/models" "gpt-load/internal/models"
"gpt-load/internal/response" "gpt-load/internal/response"
@@ -40,7 +41,7 @@ func GetSettings(c *gin.Context) {
func UpdateSettings(c *gin.Context) { func UpdateSettings(c *gin.Context) {
var settingsMap map[string]any var settingsMap map[string]any
if err := c.ShouldBindJSON(&settingsMap); err != nil { 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 return
} }
@@ -53,14 +54,14 @@ func UpdateSettings(c *gin.Context) {
// 更新配置 // 更新配置
if err := settingsManager.UpdateSettings(settingsMap); err != nil { 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 return
} }
// 重载系统配置 // 重载系统配置
if err := settingsManager.LoadFromDatabase(); err != nil { if err := settingsManager.LoadFromDatabase(); err != nil {
logrus.Errorf("Failed to reload system settings: %v", err) 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 return
} }

View File

@@ -6,9 +6,11 @@ import (
"strings" "strings"
"time" "time"
"gpt-load/internal/errors" "gpt-load/internal/response"
"gpt-load/internal/types" "gpt-load/internal/types"
app_errors "gpt-load/internal/errors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -140,20 +142,14 @@ func Auth(config types.AuthConfig) gin.HandlerFunc {
// Extract key from multiple sources // Extract key from multiple sources
key := extractKey(c) key := extractKey(c)
if key == "" { if key == "" {
c.JSON(401, gin.H{ response.Error(c, app_errors.ErrUnauthorized)
"error": "Authorization required",
"code": errors.ErrAuthMissing,
})
c.Abort() c.Abort()
return return
} }
// Validate key // Validate key
if key != config.Key { if key != config.Key {
c.JSON(401, gin.H{ response.Error(c, app_errors.ErrUnauthorized)
"error": "Invalid authentication token",
"code": errors.ErrAuthInvalid,
})
c.Abort() c.Abort()
return return
} }
@@ -165,19 +161,8 @@ func Auth(config types.AuthConfig) gin.HandlerFunc {
// Recovery creates a recovery middleware with custom error handling // Recovery creates a recovery middleware with custom error handling
func Recovery() gin.HandlerFunc { func Recovery() gin.HandlerFunc {
return gin.CustomRecovery(func(c *gin.Context, recovered any) { return gin.CustomRecovery(func(c *gin.Context, recovered any) {
if err, ok := recovered.(string); ok {
logrus.Errorf("Panic recovered: %s", err)
c.JSON(500, gin.H{
"error": "Internal server error",
"code": errors.ErrServerInternal,
})
} else {
logrus.Errorf("Panic recovered: %v", recovered) logrus.Errorf("Panic recovered: %v", recovered)
c.JSON(500, gin.H{ response.Error(c, app_errors.ErrInternalServer)
"error": "Internal server error",
"code": errors.ErrServerInternal,
})
}
c.Abort() c.Abort()
}) })
} }
@@ -193,10 +178,7 @@ func RateLimiter(config types.PerformanceConfig) gin.HandlerFunc {
defer func() { <-semaphore }() defer func() { <-semaphore }()
c.Next() c.Next()
default: default:
c.JSON(429, gin.H{ response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, "Too many concurrent requests"))
"error": "Too many concurrent requests",
"code": errors.ErrServerUnavailable,
})
c.Abort() c.Abort()
} }
} }
@@ -212,20 +194,14 @@ func ErrorHandler() gin.HandlerFunc {
err := c.Errors.Last().Err err := c.Errors.Last().Err
// Check if it's our custom error type // Check if it's our custom error type
if appErr, ok := err.(*errors.AppError); ok { if apiErr, ok := err.(*app_errors.APIError); ok {
c.JSON(appErr.HTTPStatus, gin.H{ response.Error(c, apiErr)
"error": appErr.Message,
"code": appErr.Code,
})
return return
} }
// Handle other errors // Handle other errors
logrus.Errorf("Unhandled error: %v", err) logrus.Errorf("Unhandled error: %v", err)
c.JSON(500, gin.H{ response.Error(c, app_errors.ErrInternalServer)
"error": "Internal server error",
"code": errors.ErrServerInternal,
})
} }
} }
} }

View File

@@ -4,9 +4,9 @@ package proxy
import ( import (
"fmt" "fmt"
"gpt-load/internal/channel" "gpt-load/internal/channel"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/models" "gpt-load/internal/models"
"gpt-load/internal/response" "gpt-load/internal/response"
"net/http"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -40,21 +40,21 @@ func (ps *ProxyServer) HandleProxy(c *gin.Context) {
// 1. Find the group by name // 1. Find the group by name
var group models.Group var group models.Group
if err := ps.DB.Preload("APIKeys").Where("name = ?", groupName).First(&group).Error; err != nil { if err := ps.DB.Preload("APIKeys").Where("name = ?", groupName).First(&group).Error; err != nil {
response.Error(c, http.StatusNotFound, fmt.Sprintf("Group '%s' not found", groupName)) response.Error(c, app_errors.ParseDBError(err))
return return
} }
// 2. Select an available API key from the group // 2. Select an available API key from the group
apiKey, err := ps.selectAPIKey(&group) apiKey, err := ps.selectAPIKey(&group)
if err != nil { if err != nil {
response.Error(c, http.StatusServiceUnavailable, err.Error()) response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, err.Error()))
return return
} }
// 3. Get the appropriate channel handler from the factory // 3. Get the appropriate channel handler from the factory
channelHandler, err := channel.GetChannel(&group) channelHandler, err := channel.GetChannel(&group)
if err != nil { if err != nil {
response.Error(c, http.StatusInternalServerError, fmt.Sprintf("Failed to get channel for group '%s': %v", groupName, err)) response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to get channel for group '%s': %v", groupName, err)))
return return
} }

View File

@@ -2,47 +2,38 @@
package response package response
import ( import (
app_errors "gpt-load/internal/errors"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Response defines the standard JSON response structure. // SuccessResponse defines the standard JSON success response structure.
type Response struct { type SuccessResponse struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
Data interface{} `json:"data,omitempty"` Data interface{} `json:"data,omitempty"`
} }
// ErrorResponse defines the standard JSON error response structure.
type ErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
}
// Success sends a standardized success response. // Success sends a standardized success response.
func Success(c *gin.Context, data interface{}) { func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{ c.JSON(http.StatusOK, SuccessResponse{
Code: 0, Code: 0,
Message: "Success", Message: "Success",
Data: data, Data: data,
}) })
} }
// Error sends a standardized error response. // Error sends a standardized error response using an APIError.
func Error(c *gin.Context, code int, message string) { func Error(c *gin.Context, apiErr *app_errors.APIError) {
c.JSON(code, Response{ c.JSON(apiErr.HTTPStatus, ErrorResponse{
Code: code, Code: apiErr.Code,
Message: message, Message: apiErr.Message,
Data: nil,
}) })
} }
// BadRequest sends a 400 Bad Request error response.
func BadRequest(c *gin.Context, message string) {
Error(c, http.StatusBadRequest, message)
}
// NotFound sends a 404 Not Found error response.
func NotFound(c *gin.Context, message string) {
Error(c, http.StatusNotFound, message)
}
// InternalError sends a 500 Internal Server Error response.
func InternalError(c *gin.Context, message string) {
Error(c, http.StatusInternalServerError, message)
}

View File

@@ -55,7 +55,7 @@ function handleSubmit() {
try { try {
loading.value = true; loading.value = true;
await http.post("/settings", form.value); await http.put("/settings", form.value);
fetchSettings(); fetchSettings();
} finally { } finally {
loading.value = false; loading.value = false;