|
1 | 1 | package siliconflow |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
4 | 7 | "net/http" |
5 | 8 | "strconv" |
6 | 9 | "strings" |
7 | 10 |
|
| 11 | + "github.com/bytedance/sonic" |
8 | 12 | "github.com/labring/aiproxy/core/common" |
9 | 13 | "github.com/labring/aiproxy/core/relay/adaptor" |
10 | 14 | relaymodel "github.com/labring/aiproxy/core/relay/model" |
11 | 15 | ) |
12 | 16 |
|
13 | 17 | type errorResponse struct { |
14 | | - Message string `json:"message"` |
15 | | - Code int `json:"code"` |
| 18 | + Message string `json:"message"` |
| 19 | + Code any `json:"code"` |
| 20 | + Error json.RawMessage `json:"error"` |
16 | 21 | } |
17 | 22 |
|
18 | 23 | func ErrorHandler(resp *http.Response) adaptor.Error { |
19 | 24 | defer resp.Body.Close() |
20 | 25 |
|
21 | | - var er errorResponse |
22 | | - |
23 | | - err := common.UnmarshalResponse(resp, &er) |
| 26 | + responseBody, err := common.GetResponseBody(resp) |
24 | 27 | if err != nil { |
25 | 28 | return relaymodel.WrapperOpenAIErrorWithMessage( |
26 | 29 | err.Error(), |
27 | | - "unmarshal_response_body_failed", |
| 30 | + "read_response_body_failed", |
28 | 31 | http.StatusInternalServerError, |
29 | 32 | ) |
30 | 33 | } |
31 | 34 |
|
32 | | - statusCode := resp.StatusCode |
33 | | - |
34 | | - if strings.Contains(er.Message, "System is really busy") { |
35 | | - statusCode = http.StatusTooManyRequests |
| 35 | + message, code, parseErr := parseErrorResponse(responseBody) |
| 36 | + if parseErr != nil { |
| 37 | + return relaymodel.WrapperOpenAIErrorWithMessage( |
| 38 | + parseErr.Error(), |
| 39 | + "unmarshal_response_body_failed", |
| 40 | + http.StatusInternalServerError, |
| 41 | + ) |
36 | 42 | } |
37 | 43 |
|
| 44 | + statusCode, code := normalizeError(resp.StatusCode, message, code) |
| 45 | + |
38 | 46 | return relaymodel.WrapperOpenAIErrorWithMessage( |
39 | | - er.Message, |
40 | | - strconv.Itoa(er.Code), |
| 47 | + message, |
| 48 | + code, |
41 | 49 | statusCode, |
42 | 50 | relaymodel.ErrorTypeUpstream, |
43 | 51 | ) |
44 | 52 | } |
| 53 | + |
| 54 | +func normalizeError(statusCode int, message string, code any) (int, any) { |
| 55 | + if strings.Contains(message, "System is really busy") { |
| 56 | + statusCode = http.StatusTooManyRequests |
| 57 | + } |
| 58 | + |
| 59 | + if isUnauthorizedMessage(message) { |
| 60 | + statusCode = http.StatusUnauthorized |
| 61 | + } |
| 62 | + |
| 63 | + if code == nil { |
| 64 | + code = relaymodel.ErrorCodeBadResponse |
| 65 | + } |
| 66 | + |
| 67 | + return statusCode, code |
| 68 | +} |
| 69 | + |
| 70 | +func isUnauthorizedMessage(message string) bool { |
| 71 | + lowerMessage := strings.ToLower(message) |
| 72 | + |
| 73 | + return strings.Contains(lowerMessage, "api key is invalid") || |
| 74 | + strings.Contains(lowerMessage, "invalid api key") || |
| 75 | + strings.Contains(lowerMessage, "api key provided is invalid") || |
| 76 | + strings.Contains(lowerMessage, "unauthorized") |
| 77 | +} |
| 78 | + |
| 79 | +func parseErrorResponse(body []byte) (string, any, error) { |
| 80 | + body = bytes.TrimSpace(body) |
| 81 | + if len(body) == 0 { |
| 82 | + return "", nil, io.EOF |
| 83 | + } |
| 84 | + |
| 85 | + var er errorResponse |
| 86 | + if err := sonic.Unmarshal(body, &er); err == nil { |
| 87 | + if nestedMessage, nestedCode := parseNestedError(er.Error); nestedMessage != "" { |
| 88 | + if er.Message == "" { |
| 89 | + er.Message = nestedMessage |
| 90 | + } |
| 91 | + |
| 92 | + if er.Code == nil { |
| 93 | + er.Code = nestedCode |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if er.Message != "" { |
| 98 | + return er.Message, normalizeErrorCode(er.Code), nil |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + var stringMessage string |
| 103 | + if err := sonic.Unmarshal(body, &stringMessage); err == nil && stringMessage != "" { |
| 104 | + return stringMessage, nil, nil |
| 105 | + } |
| 106 | + |
| 107 | + return string(body), nil, nil |
| 108 | +} |
| 109 | + |
| 110 | +func parseNestedError(raw json.RawMessage) (string, any) { |
| 111 | + if len(raw) == 0 { |
| 112 | + return "", nil |
| 113 | + } |
| 114 | + |
| 115 | + var message string |
| 116 | + if err := sonic.Unmarshal([]byte(raw), &message); err == nil { |
| 117 | + return message, nil |
| 118 | + } |
| 119 | + |
| 120 | + var nested struct { |
| 121 | + Message string `json:"message"` |
| 122 | + Code any `json:"code"` |
| 123 | + Error string `json:"error"` |
| 124 | + } |
| 125 | + |
| 126 | + if err := sonic.Unmarshal([]byte(raw), &nested); err == nil { |
| 127 | + if nested.Message != "" { |
| 128 | + return nested.Message, nested.Code |
| 129 | + } |
| 130 | + |
| 131 | + if nested.Error != "" { |
| 132 | + return nested.Error, nested.Code |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return "", nil |
| 137 | +} |
| 138 | + |
| 139 | +func normalizeErrorCode(code any) any { |
| 140 | + switch value := code.(type) { |
| 141 | + case nil: |
| 142 | + return nil |
| 143 | + case float64: |
| 144 | + if value == float64(int64(value)) { |
| 145 | + return strconv.FormatInt(int64(value), 10) |
| 146 | + } |
| 147 | + return value |
| 148 | + case int: |
| 149 | + return strconv.Itoa(value) |
| 150 | + case int64: |
| 151 | + return strconv.FormatInt(value, 10) |
| 152 | + default: |
| 153 | + return value |
| 154 | + } |
| 155 | +} |
0 commit comments