error.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  29. common.SysLog(fmt.Sprintf("error: %s", text))
  30. text = "请求上游地址失败"
  31. }
  32. openAIError := dto.OpenAIError{
  33. Message: text,
  34. Type: "new_api_error",
  35. Code: code,
  36. }
  37. return &dto.OpenAIErrorWithStatusCode{
  38. Error: openAIError,
  39. StatusCode: statusCode,
  40. }
  41. }
  42. func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
  43. openaiErr := OpenAIErrorWrapper(err, code, statusCode)
  44. openaiErr.LocalError = true
  45. return openaiErr
  46. }
  47. func ClaudeErrorWrapper(err error, code string, statusCode int) *dto.ClaudeErrorWithStatusCode {
  48. text := err.Error()
  49. lowerText := strings.ToLower(text)
  50. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  51. common.SysLog(fmt.Sprintf("error: %s", text))
  52. text = "请求上游地址失败"
  53. }
  54. claudeError := dto.ClaudeError{
  55. Message: text,
  56. Type: "new_api_error",
  57. //Code: code,
  58. }
  59. return &dto.ClaudeErrorWithStatusCode{
  60. Error: claudeError,
  61. StatusCode: statusCode,
  62. }
  63. }
  64. func ClaudeErrorWrapperLocal(err error, code string, statusCode int) *dto.ClaudeErrorWithStatusCode {
  65. claudeErr := ClaudeErrorWrapper(err, code, statusCode)
  66. claudeErr.LocalError = true
  67. return claudeErr
  68. }
  69. func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (errWithStatusCode *dto.OpenAIErrorWithStatusCode) {
  70. errWithStatusCode = &dto.OpenAIErrorWithStatusCode{
  71. StatusCode: resp.StatusCode,
  72. Error: dto.OpenAIError{
  73. Type: "upstream_error",
  74. Code: "bad_response_status_code",
  75. Param: strconv.Itoa(resp.StatusCode),
  76. },
  77. }
  78. responseBody, err := io.ReadAll(resp.Body)
  79. if err != nil {
  80. return
  81. }
  82. err = resp.Body.Close()
  83. if err != nil {
  84. return
  85. }
  86. var errResponse dto.GeneralErrorResponse
  87. err = json.Unmarshal(responseBody, &errResponse)
  88. if err != nil {
  89. if showBodyWhenFail {
  90. errWithStatusCode.Error.Message = string(responseBody)
  91. } else {
  92. errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
  93. }
  94. return
  95. }
  96. if errResponse.Error.Message != "" {
  97. // OpenAI format error, so we override the default one
  98. errWithStatusCode.Error = errResponse.Error
  99. } else {
  100. errWithStatusCode.Error.Message = errResponse.ToMessage()
  101. }
  102. if errWithStatusCode.Error.Message == "" {
  103. errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
  104. }
  105. return
  106. }
  107. func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMappingStr string) {
  108. if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
  109. return
  110. }
  111. statusCodeMapping := make(map[string]string)
  112. err := json.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
  113. if err != nil {
  114. return
  115. }
  116. if openaiErr.StatusCode == http.StatusOK {
  117. return
  118. }
  119. codeStr := strconv.Itoa(openaiErr.StatusCode)
  120. if _, ok := statusCodeMapping[codeStr]; ok {
  121. intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
  122. openaiErr.StatusCode = intCode
  123. }
  124. }
  125. func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
  126. openaiErr := TaskErrorWrapper(err, code, statusCode)
  127. openaiErr.LocalError = true
  128. return openaiErr
  129. }
  130. func TaskErrorWrapper(err error, code string, statusCode int) *dto.TaskError {
  131. text := err.Error()
  132. lowerText := strings.ToLower(text)
  133. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  134. common.SysLog(fmt.Sprintf("error: %s", text))
  135. text = "请求上游地址失败"
  136. }
  137. //避免暴露内部错误
  138. taskError := &dto.TaskError{
  139. Code: code,
  140. Message: text,
  141. StatusCode: statusCode,
  142. Error: err,
  143. }
  144. return taskError
  145. }