error.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) (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. return
  68. }
  69. if errResponse.Error.Message != "" {
  70. // OpenAI format error, so we override the default one
  71. errWithStatusCode.Error = errResponse.Error
  72. } else {
  73. errWithStatusCode.Error.Message = errResponse.ToMessage()
  74. }
  75. if errWithStatusCode.Error.Message == "" {
  76. errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
  77. }
  78. return
  79. }
  80. func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMappingStr string) {
  81. if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
  82. return
  83. }
  84. statusCodeMapping := make(map[string]string)
  85. err := json.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
  86. if err != nil {
  87. return
  88. }
  89. if openaiErr.StatusCode == http.StatusOK {
  90. return
  91. }
  92. codeStr := strconv.Itoa(openaiErr.StatusCode)
  93. if _, ok := statusCodeMapping[codeStr]; ok {
  94. intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
  95. openaiErr.StatusCode = intCode
  96. }
  97. }
  98. func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
  99. openaiErr := TaskErrorWrapper(err, code, statusCode)
  100. openaiErr.LocalError = true
  101. return openaiErr
  102. }
  103. func TaskErrorWrapper(err error, code string, statusCode int) *dto.TaskError {
  104. text := err.Error()
  105. lowerText := strings.ToLower(text)
  106. if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
  107. common.SysLog(fmt.Sprintf("error: %s", text))
  108. text = "请求上游地址失败"
  109. }
  110. //避免暴露内部错误
  111. taskError := &dto.TaskError{
  112. Code: code,
  113. Message: text,
  114. StatusCode: statusCode,
  115. Error: err,
  116. }
  117. return taskError
  118. }