This commit is contained in:
Mrx
2026-03-25 15:03:45 +08:00
parent 9438ccee5e
commit 5f0224ad3b
2 changed files with 29 additions and 6 deletions

View File

@@ -4,6 +4,17 @@ import (
"fmt"
)
// GetQueryEmptyErrByCode 将数据宝错误码归类为“查询为空/不扣费”错误。
// 说明:上游通常依赖 errors.Is(err, ErrQueryEmpty) 来决定是否扣费。
func GetQueryEmptyErrByCode(code string) error {
switch code {
case "10001", "10006":
return ErrQueryEmpty
default:
return nil
}
}
// ShujubaoError 数据宝服务错误
type ShujubaoError struct {
Code string `json:"code"`

View File

@@ -292,14 +292,26 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
}
code := shujubaoResp.Code
if code == "10001" || code == "10006" {
// 查空/查无:返回空数组,不视为错误
return []interface{}{}, nil
// 成功码只有这三类:其它 code 都走统一错误映射返回
if code != "10000" && code != "200" && code != "0" {
// 将上游失败返回码单独记录到日志参数中,便于排查与统计
logParams := paramsForLog(params)
if logParams == nil {
logParams = map[string]interface{}{}
}
if code != "10000" {
logParams["shujubao_return_code"] = code
shujubaoErr := NewShujubaoErrorFromCode(code, shujubaoResp.Message)
if queryEmptyErr := GetQueryEmptyErrByCode(code); queryEmptyErr != nil {
err = errors.Join(queryEmptyErr, shujubaoErr)
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, shujubaoErr, paramsForLog(params))
s.logger.LogError(requestID, transactionID, apiPath, err, logParams)
}
return nil, err
}
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, shujubaoErr, logParams)
}
return nil, errors.Join(ErrDatasource, shujubaoErr)
}