|
- package metrics
-
- import (
- "strconv"
- "strings"
- )
-
- // ClassifyError 根据状态码和错误消息分类错误
- func ClassifyError(statusCode int, errMsg string) (string, string) {
- // 超时类
- if strings.Contains(errMsg, "context deadline exceeded") {
- return "timeout", "context_deadline_exceeded"
- }
- if strings.Contains(errMsg, "context canceled") {
- return "timeout", "context_canceled"
- }
-
- // 配额/计费类
- if statusCode == 402 {
- return "billing", "insufficient_quota"
- }
- if statusCode == 403 && strings.Contains(strings.ToLower(errMsg), "quota") {
- return "billing", "quota_exceeded"
- }
-
- // 认证类
- if statusCode == 401 {
- return "auth", "401"
- }
- if statusCode == 403 && !strings.Contains(strings.ToLower(errMsg), "quota") {
- return "auth", "403"
- }
-
- // 速率限制
- if statusCode == 429 {
- return "rate_limit", "429"
- }
-
- // 上游错误(5xx)
- if statusCode >= 500 {
- return "upstream", strconv.Itoa(statusCode)
- }
- if statusCode >= 400 {
- return "other", strconv.Itoa(statusCode)
- }
-
- // 无状态码的未知错误
- if statusCode == 0 {
- return "other", "unknown"
- }
-
- return "other", strconv.Itoa(statusCode)
- }
|