error.go 901 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package siliconflow
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/labring/aiproxy/core/common"
  7. "github.com/labring/aiproxy/core/relay/adaptor"
  8. relaymodel "github.com/labring/aiproxy/core/relay/model"
  9. )
  10. type errorResponse struct {
  11. Message string `json:"message"`
  12. Code int `json:"code"`
  13. }
  14. func ErrorHandler(resp *http.Response) adaptor.Error {
  15. defer resp.Body.Close()
  16. var er errorResponse
  17. err := common.UnmarshalResponse(resp, &er)
  18. if err != nil {
  19. return relaymodel.WrapperOpenAIErrorWithMessage(
  20. err.Error(),
  21. "unmarshal_response_body_failed",
  22. http.StatusInternalServerError,
  23. )
  24. }
  25. statusCode := resp.StatusCode
  26. if strings.Contains(er.Message, "System is really busy") {
  27. statusCode = http.StatusTooManyRequests
  28. }
  29. return relaymodel.WrapperOpenAIErrorWithMessage(
  30. er.Message,
  31. strconv.Itoa(er.Code),
  32. statusCode,
  33. relaymodel.ErrorTypeUpstream,
  34. )
  35. }