fix: enhance error handling for invalid request types in relay handlers
This commit is contained in:
@@ -2,12 +2,13 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/constant"
|
"one-api/constant"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const KeyRequestBody = "key_request_body"
|
const KeyRequestBody = "key_request_body"
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ
|
|||||||
claudeReq, ok := info.Request.(*dto.ClaudeRequest)
|
claudeReq, ok := info.Request.(*dto.ClaudeRequest)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected *dto.ClaudeRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.ClaudeRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(claudeReq)
|
request, err := common.DeepCopy(claudeReq)
|
||||||
|
|||||||
@@ -29,8 +29,7 @@ func TextHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types
|
|||||||
|
|
||||||
textReq, ok := info.Request.(*dto.GeneralOpenAIRequest)
|
textReq, ok := info.Request.(*dto.GeneralOpenAIRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
//return types.NewErrorWithStatusCode(errors.New("invalid request type"), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.GeneralOpenAIRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
common.FatalLog("invalid request type, expected dto.GeneralOpenAIRequest, got %T", info.Request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(textReq)
|
request, err := common.DeepCopy(textReq)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func EmbeddingHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *
|
|||||||
|
|
||||||
embeddingReq, ok := info.Request.(*dto.EmbeddingRequest)
|
embeddingReq, ok := info.Request.(*dto.EmbeddingRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected *dto.EmbeddingRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.EmbeddingRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(embeddingReq)
|
request, err := common.DeepCopy(embeddingReq)
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func GeminiHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ
|
|||||||
|
|
||||||
geminiReq, ok := info.Request.(*dto.GeminiChatRequest)
|
geminiReq, ok := info.Request.(*dto.GeminiChatRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected *dto.GeminiChatRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.GeminiChatRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(geminiReq)
|
request, err := common.DeepCopy(geminiReq)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func ImageHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *type
|
|||||||
|
|
||||||
imageReq, ok := info.Request.(*dto.ImageRequest)
|
imageReq, ok := info.Request.(*dto.ImageRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected dto.ImageRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.ImageRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(imageReq)
|
request, err := common.DeepCopy(imageReq)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func RerankHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ
|
|||||||
|
|
||||||
rerankReq, ok := info.Request.(*dto.RerankRequest)
|
rerankReq, ok := info.Request.(*dto.RerankRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected dto.RerankRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.RerankRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(rerankReq)
|
request, err := common.DeepCopy(rerankReq)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func ResponsesHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *
|
|||||||
|
|
||||||
responsesReq, ok := info.Request.(*dto.OpenAIResponsesRequest)
|
responsesReq, ok := info.Request.(*dto.OpenAIResponsesRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
common.FatalLog(fmt.Sprintf("invalid request type, expected dto.OpenAIResponsesRequest, got %T", info.Request))
|
return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.OpenAIResponsesRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := common.DeepCopy(responsesReq)
|
request, err := common.DeepCopy(responsesReq)
|
||||||
|
|||||||
Reference in New Issue
Block a user