error.go 4.6 KB

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