error.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. common.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. newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
  88. }
  89. return
  90. }
  91. if errResponse.Error.Message != "" {
  92. // General format error (OpenAI, Anthropic, Gemini, etc.)
  93. newApiErr = types.WithOpenAIError(errResponse.Error, resp.StatusCode)
  94. } else {
  95. newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
  96. }
  97. return
  98. }
  99. func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string) {
  100. if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
  101. return
  102. }
  103. statusCodeMapping := make(map[string]string)
  104. err := common.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
  105. if err != nil {
  106. return
  107. }
  108. if newApiErr.StatusCode == http.StatusOK {
  109. return
  110. }
  111. codeStr := strconv.Itoa(newApiErr.StatusCode)
  112. if _, ok := statusCodeMapping[codeStr]; ok {
  113. intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
  114. newApiErr.StatusCode = intCode
  115. }
  116. }
  117. func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
  118. openaiErr := TaskErrorWrapper(err, code, statusCode)
  119. openaiErr.LocalError = true
  120. return openaiErr
  121. }
  122. func TaskErrorWrapper(err error, code string, statusCode int) *dto.TaskError {
  123. text := err.Error()
  124. lowerText := strings.ToLower(text)
  125. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  126. common.SysLog(fmt.Sprintf("error: %s", text))
  127. text = "请求上游地址失败"
  128. }
  129. //避免暴露内部错误
  130. taskError := &dto.TaskError{
  131. Code: code,
  132. Message: text,
  133. StatusCode: statusCode,
  134. Error: err,
  135. }
  136. return taskError
  137. }