error.go 5.3 KB

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