This commit refactors the application's error handling mechanism by introducing a new standardized error type, `types.NewAPIError`. It also renames common JSON utility functions for better clarity. Previously, internal error handling was tightly coupled to the `dto.OpenAIError` format. This change decouples the internal logic from the external API representation. Key changes: - A new `types.NewAPIError` struct is introduced to serve as a canonical internal representation for all API errors. - All relay adapters (OpenAI, Claude, Gemini, etc.) are updated to return `*types.NewAPIError`. - Controllers now convert the internal `NewAPIError` to the client-facing `OpenAIError` format at the API boundary, ensuring backward compatibility. - Channel auto-disable/enable logic is updated to use the new standardized error type. - JSON utility functions are renamed to align with Go's standard library conventions (e.g., `UnmarshalJson` -> `Unmarshal`, `EncodeJson` -> `Marshal`).
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package relay
|
|
|
|
import (
|
|
"fmt"
|
|
"one-api/dto"
|
|
relaycommon "one-api/relay/common"
|
|
"one-api/relay/helper"
|
|
"one-api/service"
|
|
"one-api/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
func WssHelper(c *gin.Context, ws *websocket.Conn) (newAPIError *types.NewAPIError) {
|
|
relayInfo := relaycommon.GenRelayInfoWs(c, ws)
|
|
|
|
// get & validate textRequest 获取并验证文本请求
|
|
//realtimeEvent, err := getAndValidateWssRequest(c, ws)
|
|
//if err != nil {
|
|
// common.LogError(c, fmt.Sprintf("getAndValidateWssRequest failed: %s", err.Error()))
|
|
// return service.OpenAIErrorWrapperLocal(err, "invalid_text_request", http.StatusBadRequest)
|
|
//}
|
|
|
|
err := helper.ModelMappedHelper(c, relayInfo, nil)
|
|
if err != nil {
|
|
return types.NewError(err, types.ErrorCodeChannelModelMappedError)
|
|
}
|
|
|
|
priceData, err := helper.ModelPriceHelper(c, relayInfo, 0, 0)
|
|
if err != nil {
|
|
return types.NewError(err, types.ErrorCodeModelPriceError)
|
|
}
|
|
|
|
// pre-consume quota 预消耗配额
|
|
preConsumedQuota, userQuota, newAPIError := preConsumeQuota(c, priceData.ShouldPreConsumedQuota, relayInfo)
|
|
if newAPIError != nil {
|
|
return newAPIError
|
|
}
|
|
|
|
defer func() {
|
|
if newAPIError != nil {
|
|
returnPreConsumedQuota(c, relayInfo, userQuota, preConsumedQuota)
|
|
}
|
|
}()
|
|
|
|
adaptor := GetAdaptor(relayInfo.ApiType)
|
|
if adaptor == nil {
|
|
return types.NewError(fmt.Errorf("invalid api type: %d", relayInfo.ApiType), types.ErrorCodeInvalidApiType)
|
|
}
|
|
adaptor.Init(relayInfo)
|
|
//var requestBody io.Reader
|
|
//firstWssRequest, _ := c.Get("first_wss_request")
|
|
//requestBody = bytes.NewBuffer(firstWssRequest.([]byte))
|
|
|
|
statusCodeMappingStr := c.GetString("status_code_mapping")
|
|
resp, err := adaptor.DoRequest(c, relayInfo, nil)
|
|
if err != nil {
|
|
return types.NewError(err, types.ErrorCodeDoRequestFailed)
|
|
}
|
|
|
|
if resp != nil {
|
|
relayInfo.TargetWs = resp.(*websocket.Conn)
|
|
defer relayInfo.TargetWs.Close()
|
|
}
|
|
|
|
usage, newAPIError := adaptor.DoResponse(c, nil, relayInfo)
|
|
if newAPIError != nil {
|
|
// reset status code 重置状态码
|
|
service.ResetStatusCode(newAPIError, statusCodeMappingStr)
|
|
return newAPIError
|
|
}
|
|
service.PostWssConsumeQuota(c, relayInfo, relayInfo.UpstreamModelName, usage.(*dto.RealtimeUsage), preConsumedQuota,
|
|
userQuota, priceData, "")
|
|
return nil
|
|
}
|