You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

54 lines
1.1 KiB

  1. package metrics
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // ClassifyError 根据状态码和错误消息分类错误
  7. func ClassifyError(statusCode int, errMsg string) (string, string) {
  8. // 超时类
  9. if strings.Contains(errMsg, "context deadline exceeded") {
  10. return "timeout", "context_deadline_exceeded"
  11. }
  12. if strings.Contains(errMsg, "context canceled") {
  13. return "timeout", "context_canceled"
  14. }
  15. // 配额/计费类
  16. if statusCode == 402 {
  17. return "billing", "insufficient_quota"
  18. }
  19. if statusCode == 403 && strings.Contains(strings.ToLower(errMsg), "quota") {
  20. return "billing", "quota_exceeded"
  21. }
  22. // 认证类
  23. if statusCode == 401 {
  24. return "auth", "401"
  25. }
  26. if statusCode == 403 && !strings.Contains(strings.ToLower(errMsg), "quota") {
  27. return "auth", "403"
  28. }
  29. // 速率限制
  30. if statusCode == 429 {
  31. return "rate_limit", "429"
  32. }
  33. // 上游错误(5xx)
  34. if statusCode >= 500 {
  35. return "upstream", strconv.Itoa(statusCode)
  36. }
  37. if statusCode >= 400 {
  38. return "other", strconv.Itoa(statusCode)
  39. }
  40. // 无状态码的未知错误
  41. if statusCode == 0 {
  42. return "other", "unknown"
  43. }
  44. return "other", strconv.Itoa(statusCode)
  45. }