error.go 4.4 KB

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