mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-14 20:23:46 +08:00
🎨 结构中继控制器
This commit is contained in:
@@ -96,7 +96,7 @@ func SendRequest(req *http.Request, response any, outputResp bool) (*http.Respon
|
||||
// 发送请求
|
||||
resp, err := HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "http_request_failed", http.StatusInternalServerError)
|
||||
return nil, ErrorWrapper(err, "http_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if !outputResp {
|
||||
@@ -120,7 +120,7 @@ func SendRequest(req *http.Request, response any, outputResp bool) (*http.Respon
|
||||
err = DecodeResponse(resp.Body, response)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "decode_response_failed", http.StatusInternalServerError)
|
||||
return nil, ErrorWrapper(err, "decode_response_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if outputResp {
|
||||
|
||||
@@ -2,11 +2,12 @@ package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func UnmarshalBodyReusable(c *gin.Context, v any) error {
|
||||
@@ -18,17 +19,43 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contentType := c.Request.Header.Get("Content-Type")
|
||||
if strings.HasPrefix(contentType, "application/json") {
|
||||
err = json.Unmarshal(requestBody, &v)
|
||||
} else if strings.HasPrefix(contentType, "multipart/form-data") {
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||
err = c.ShouldBind(v)
|
||||
}
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||
err = c.ShouldBind(v)
|
||||
if err != nil {
|
||||
if errs, ok := err.(validator.ValidationErrors); ok {
|
||||
// 返回第一个错误字段的名称
|
||||
return fmt.Errorf("field %s is required", errs[0].Field())
|
||||
}
|
||||
return err
|
||||
}
|
||||
// Reset request body
|
||||
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ErrorWrapper(err error, code string, statusCode int) *types.OpenAIErrorWithStatusCode {
|
||||
return StringErrorWrapper(err.Error(), code, statusCode)
|
||||
}
|
||||
|
||||
func StringErrorWrapper(err string, code string, statusCode int) *types.OpenAIErrorWithStatusCode {
|
||||
openAIError := types.OpenAIError{
|
||||
Message: err,
|
||||
Type: "one_api_error",
|
||||
Code: code,
|
||||
}
|
||||
return &types.OpenAIErrorWithStatusCode{
|
||||
OpenAIError: openAIError,
|
||||
StatusCode: statusCode,
|
||||
}
|
||||
}
|
||||
|
||||
func AbortWithMessage(c *gin.Context, statusCode int, message string) {
|
||||
c.JSON(statusCode, gin.H{
|
||||
"error": gin.H{
|
||||
"message": message,
|
||||
"type": "one_api_error",
|
||||
},
|
||||
})
|
||||
c.Abort()
|
||||
LogError(c.Request.Context(), message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user