error.go 3.7 KB

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