error.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/dto"
  9. "one-api/logger"
  10. "one-api/types"
  11. "strconv"
  12. "strings"
  13. )
  14. func MidjourneyErrorWrapper(code int, desc string) *dto.MidjourneyResponse {
  15. return &dto.MidjourneyResponse{
  16. Code: code,
  17. Description: desc,
  18. }
  19. }
  20. func MidjourneyErrorWithStatusCodeWrapper(code int, desc string, statusCode int) *dto.MidjourneyResponseWithStatusCode {
  21. return &dto.MidjourneyResponseWithStatusCode{
  22. StatusCode: statusCode,
  23. Response: *MidjourneyErrorWrapper(code, desc),
  24. }
  25. }
  26. //// OpenAIErrorWrapper wraps an error into an OpenAIErrorWithStatusCode
  27. //func OpenAIErrorWrapper(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
  28. // text := err.Error()
  29. // lowerText := strings.ToLower(text)
  30. // if !strings.HasPrefix(lowerText, "get file base64 from url") && !strings.HasPrefix(lowerText, "mime type is not supported") {
  31. // if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  32. // common.SysLog(fmt.Sprintf("error: %s", text))
  33. // text = "请求上游地址失败"
  34. // }
  35. // }
  36. // openAIError := dto.OpenAIError{
  37. // Message: text,
  38. // Type: "new_api_error",
  39. // Code: code,
  40. // }
  41. // return &dto.OpenAIErrorWithStatusCode{
  42. // Error: openAIError,
  43. // StatusCode: statusCode,
  44. // }
  45. //}
  46. //
  47. //func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
  48. // openaiErr := OpenAIErrorWrapper(err, code, statusCode)
  49. // openaiErr.LocalError = true
  50. // return openaiErr
  51. //}
  52. func ClaudeErrorWrapper(err error, code string, statusCode int) *dto.ClaudeErrorWithStatusCode {
  53. text := err.Error()
  54. lowerText := strings.ToLower(text)
  55. if !strings.HasPrefix(lowerText, "get file base64 from url") {
  56. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  57. logger.SysLog(fmt.Sprintf("error: %s", text))
  58. text = "请求上游地址失败"
  59. }
  60. }
  61. claudeError := types.ClaudeError{
  62. Message: text,
  63. Type: "new_api_error",
  64. }
  65. return &dto.ClaudeErrorWithStatusCode{
  66. Error: claudeError,
  67. StatusCode: statusCode,
  68. }
  69. }
  70. func ClaudeErrorWrapperLocal(err error, code string, statusCode int) *dto.ClaudeErrorWithStatusCode {
  71. claudeErr := ClaudeErrorWrapper(err, code, statusCode)
  72. claudeErr.LocalError = true
  73. return claudeErr
  74. }
  75. func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (newApiErr *types.NewAPIError) {
  76. newApiErr = types.InitOpenAIError(types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
  77. responseBody, err := io.ReadAll(resp.Body)
  78. if err != nil {
  79. return
  80. }
  81. CloseResponseBodyGracefully(resp)
  82. var errResponse dto.GeneralErrorResponse
  83. err = common.Unmarshal(responseBody, &errResponse)
  84. if err != nil {
  85. if showBodyWhenFail {
  86. newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
  87. } else {
  88. if common.DebugEnabled {
  89. println(fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody)))
  90. }
  91. newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
  92. }
  93. return
  94. }
  95. if errResponse.Error.Message != "" {
  96. // General format error (OpenAI, Anthropic, Gemini, etc.)
  97. newApiErr = types.WithOpenAIError(errResponse.Error, resp.StatusCode)
  98. } else {
  99. newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
  100. }
  101. return
  102. }
  103. func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string) {
  104. if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
  105. return
  106. }
  107. statusCodeMapping := make(map[string]string)
  108. err := common.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
  109. if err != nil {
  110. return
  111. }
  112. if newApiErr.StatusCode == http.StatusOK {
  113. return
  114. }
  115. codeStr := strconv.Itoa(newApiErr.StatusCode)
  116. if _, ok := statusCodeMapping[codeStr]; ok {
  117. intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
  118. newApiErr.StatusCode = intCode
  119. }
  120. }
  121. func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
  122. openaiErr := TaskErrorWrapper(err, code, statusCode)
  123. openaiErr.LocalError = true
  124. return openaiErr
  125. }
  126. func TaskErrorWrapper(err error, code string, statusCode int) *dto.TaskError {
  127. text := err.Error()
  128. lowerText := strings.ToLower(text)
  129. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  130. logger.SysLog(fmt.Sprintf("error: %s", text))
  131. text = "请求上游地址失败"
  132. }
  133. //避免暴露内部错误
  134. taskError := &dto.TaskError{
  135. Code: code,
  136. Message: text,
  137. StatusCode: statusCode,
  138. Error: err,
  139. }
  140. return taskError
  141. }