mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-26 11:56:38 +08:00
错误处理中的资源未释放: 在多个错误处理函数中,HTTP响应体可能未正确关闭: error.go:53-85 虽然调用了,但如果在此之前发生错误,资源可能泄露。resp.Body.Close()
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"one-api/common"
|
|
"one-api/dto"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func MidjourneyErrorWrapper(code int, desc string) *dto.MidjourneyResponse {
|
|
return &dto.MidjourneyResponse{
|
|
Code: code,
|
|
Description: desc,
|
|
}
|
|
}
|
|
|
|
func MidjourneyErrorWithStatusCodeWrapper(code int, desc string, statusCode int) *dto.MidjourneyResponseWithStatusCode {
|
|
return &dto.MidjourneyResponseWithStatusCode{
|
|
StatusCode: statusCode,
|
|
Response: *MidjourneyErrorWrapper(code, desc),
|
|
}
|
|
}
|
|
|
|
// OpenAIErrorWrapper wraps an error into an OpenAIErrorWithStatusCode
|
|
func OpenAIErrorWrapper(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
|
text := err.Error()
|
|
lowerText := strings.ToLower(text)
|
|
if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
|
|
common.SysLog(fmt.Sprintf("error: %s", text))
|
|
text = "请求上游地址失败"
|
|
}
|
|
openAIError := dto.OpenAIError{
|
|
Message: text,
|
|
Type: "new_api_error",
|
|
Code: code,
|
|
}
|
|
return &dto.OpenAIErrorWithStatusCode{
|
|
Error: openAIError,
|
|
StatusCode: statusCode,
|
|
}
|
|
}
|
|
|
|
func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
|
openaiErr := OpenAIErrorWrapper(err, code, statusCode)
|
|
openaiErr.LocalError = true
|
|
return openaiErr
|
|
}
|
|
|
|
func cacheSetUserName(userId int, username string) {
|
|
if !common.RedisEnabled {
|
|
return
|
|
}
|
|
key := fmt.Sprintf("user_name:%d", userId)
|
|
err := common.RedisSet(key, username, time.Duration(UserId2UsernameCacheSeconds)*time.Second)
|
|
if err != nil {
|
|
common.SysError("Redis set user name error: " + err.Error()) // 修正错误信息
|
|
}
|
|
}
|
|
|
|
func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMappingStr string) {
|
|
if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
|
|
return
|
|
}
|
|
statusCodeMapping := make(map[string]string)
|
|
err := json.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if openaiErr.StatusCode == http.StatusOK {
|
|
return
|
|
}
|
|
codeStr := strconv.Itoa(openaiErr.StatusCode)
|
|
if _, ok := statusCodeMapping[codeStr]; ok {
|
|
intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
|
|
openaiErr.StatusCode = intCode
|
|
}
|
|
}
|
|
|
|
func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
|
|
openaiErr := TaskErrorWrapper(err, code, statusCode)
|
|
openaiErr.LocalError = true
|
|
return openaiErr
|
|
}
|
|
|
|
func TaskErrorWrapper(err error, code string, statusCode int) *dto.TaskError {
|
|
text := err.Error()
|
|
lowerText := strings.ToLower(text)
|
|
if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
|
|
common.SysLog(fmt.Sprintf("error: %s", text))
|
|
text = "请求上游地址失败"
|
|
}
|
|
//避免暴露内部错误
|
|
taskError := &dto.TaskError{
|
|
Code: code,
|
|
Message: text,
|
|
StatusCode: statusCode,
|
|
Error: err,
|
|
}
|
|
|
|
return taskError
|
|
}
|