error.go 4.6 KB

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