fix(realname): 业务 code 优先于 HTTP 状态码,并区分额度耗尽
实测服务商业务失败时 HTTP 状态码会跟着变(身份证号错误返回 400 且 result 为 null),按状态码判断会把第三方异常误判成入参错误。 改为以业务 code 为准,仅在拿不到 code 时退回状态码与网关错误头。 额度耗尽(x-ca-error-code=B403MQ)此前与限流同样提示「请求过于频繁, 请稍后再试」,但重试不会恢复,新增 ErrProviderQuotaExhausted 并提示 联系客服。网关错误原因只在响应头,一并带进错误信息便于排查。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -108,8 +108,15 @@ func (p *CloudMarketProvider) Start(ctx context.Context, req StartRequest) (Prov
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 网关层错误不会带业务 code,原因只在这两个响应头里。
|
||||
gatewayCode := strings.TrimSpace(resp.Header.Get("X-Ca-Error-Code"))
|
||||
gatewayMessage := strings.TrimSpace(resp.Header.Get("X-Ca-Error-Message"))
|
||||
|
||||
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusForbidden {
|
||||
return ProviderResult{}, ErrProviderRateLimited
|
||||
if isQuotaExhausted(gatewayCode, gatewayMessage) {
|
||||
return ProviderResult{}, fmt.Errorf("%w: cloud market %s: %s", ErrProviderQuotaExhausted, gatewayCode, gatewayMessage)
|
||||
}
|
||||
return ProviderResult{}, fmt.Errorf("%w: cloud market %s: %s", ErrProviderRateLimited, gatewayCode, gatewayMessage)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
@@ -119,16 +126,16 @@ func (p *CloudMarketProvider) Start(ctx context.Context, req StartRequest) (Prov
|
||||
|
||||
var payload cloudMarketResponse
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return ProviderResult{}, cloudMarketHTTPError(resp.StatusCode, "")
|
||||
}
|
||||
return ProviderResult{}, fmt.Errorf("%w: invalid cloud market response: %v", ErrProviderUnavailable, err)
|
||||
payload = cloudMarketResponse{}
|
||||
}
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return ProviderResult{}, cloudMarketHTTPError(resp.StatusCode, payload.Message)
|
||||
|
||||
// 服务商失败时 HTTP 状态码也会跟着变(如身份证号错误返回 400),
|
||||
// 但业务 code 才是权威结果,只有拿不到 code 时才退回按状态码判断。
|
||||
code := strings.TrimSpace(string(payload.Code))
|
||||
if code == "" {
|
||||
return ProviderResult{}, cloudMarketHTTPError(resp.StatusCode, firstNonEmpty(gatewayMessage, truncateForLog(body)))
|
||||
}
|
||||
// 先判断 code,code 非 0 表示调用失败,无需再看 result。
|
||||
if code := string(payload.Code); code != "0" {
|
||||
if code != "0" {
|
||||
return ProviderResult{}, cloudMarketCodeError(code, payload.Message)
|
||||
}
|
||||
|
||||
@@ -171,16 +178,36 @@ func (p *CloudMarketProvider) Start(ctx context.Context, req StartRequest) (Prov
|
||||
return base, nil
|
||||
}
|
||||
|
||||
func cloudMarketHTTPError(statusCode int, message string) error {
|
||||
message = strings.TrimSpace(message)
|
||||
switch statusCode {
|
||||
case http.StatusBadRequest:
|
||||
return fmt.Errorf("%w: %s", ErrInvalidRealnameInput, firstNonEmpty(message, "cloud market bad request"))
|
||||
case http.StatusUnauthorized, http.StatusNotFound:
|
||||
return fmt.Errorf("%w: cloud market http status %d: %s", ErrProviderConfigInvalid, statusCode, message)
|
||||
default:
|
||||
return fmt.Errorf("%w: cloud market http status %d: %s", ErrProviderUnavailable, statusCode, message)
|
||||
// isQuotaExhausted 区分额度耗尽与调用频率过高:两者都是 HTTP 403,但额度耗尽靠重试不会恢复。
|
||||
func isQuotaExhausted(gatewayCode, gatewayMessage string) bool {
|
||||
if strings.EqualFold(gatewayCode, "B403MQ") {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(strings.ToLower(gatewayMessage), "quota exhausted")
|
||||
}
|
||||
|
||||
func cloudMarketHTTPError(statusCode int, detail string) error {
|
||||
detail = strings.TrimSpace(detail)
|
||||
switch {
|
||||
case statusCode == http.StatusBadRequest:
|
||||
return fmt.Errorf("%w: %s", ErrInvalidRealnameInput, firstNonEmpty(detail, "cloud market bad request"))
|
||||
case statusCode == http.StatusUnauthorized, statusCode == http.StatusNotFound:
|
||||
return fmt.Errorf("%w: cloud market http status %d: %s", ErrProviderConfigInvalid, statusCode, detail)
|
||||
case statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices:
|
||||
return fmt.Errorf("%w: cloud market response without code: %s", ErrProviderUnavailable, detail)
|
||||
default:
|
||||
return fmt.Errorf("%w: cloud market http status %d: %s", ErrProviderUnavailable, statusCode, detail)
|
||||
}
|
||||
}
|
||||
|
||||
func truncateForLog(body []byte) string {
|
||||
const limit = 200
|
||||
text := strings.TrimSpace(string(body))
|
||||
runes := []rune(text)
|
||||
if len(runes) <= limit {
|
||||
return text
|
||||
}
|
||||
return string(runes[:limit]) + "..."
|
||||
}
|
||||
|
||||
// cloudMarketCodeError 按服务商错误码表映射,code 取值见 docs/实名认证.md。
|
||||
|
||||
Reference in New Issue
Block a user