This commit is contained in:
Mrx
2026-02-08 12:32:26 +08:00
parent c9126dd780
commit 3eae08a576

View File

@@ -188,7 +188,7 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
if err != nil {
err = errors.Join(ErrSystem, err)
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, nil)
s.logger.LogError(requestID, transactionID, apiPath, err, map[string]interface{}{"request_params": reqFormData})
}
return nil, err
}
@@ -216,7 +216,7 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
err = errors.Join(ErrSystem, err)
}
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, nil)
s.logger.LogError(requestID, transactionID, apiPath, err, map[string]interface{}{"request_params": reqFormData})
}
return nil, err
}
@@ -227,7 +227,7 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
if err != nil {
err = errors.Join(ErrSystem, err)
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, nil)
s.logger.LogError(requestID, transactionID, apiPath, err, map[string]interface{}{"request_params": reqFormData})
}
return nil, err
}
@@ -235,21 +235,11 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
if resp.StatusCode != http.StatusOK {
err = errors.Join(ErrDatasource, fmt.Errorf("HTTP %d", resp.StatusCode))
if s.logger != nil {
var errorResponse interface{} = string(raw)
// 尝试解析 JSON 获取 msg
var tempResp ShumaiResponse
if json.Unmarshal(raw, &tempResp) == nil {
msg := tempResp.Msg
if msg == "" {
msg = tempResp.Message
}
if msg != "" {
errorResponse = map[string]interface{}{
"msg": msg,
}
}
errorPayload := map[string]interface{}{
"request_params": reqFormData,
"response_body": string(raw),
}
s.logger.LogError(requestID, transactionID, apiPath, err, errorResponse)
s.logger.LogError(requestID, transactionID, apiPath, err, errorPayload)
}
return nil, err
}
@@ -260,11 +250,14 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
var shumaiResp ShumaiResponse
if err := json.Unmarshal(raw, &shumaiResp); err != nil {
err = errors.Join(ErrSystem, fmt.Errorf("响应解析失败: %w", err))
parseErr := errors.Join(ErrSystem, fmt.Errorf("响应解析失败: %w", err))
if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, string(raw))
s.logger.LogError(requestID, transactionID, apiPath, parseErr, map[string]interface{}{
"request_params": reqFormData,
"response_body": string(raw),
})
}
return nil, err
return nil, parseErr
}
codeStr := strconv.Itoa(shumaiResp.Code)
@@ -279,10 +272,10 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
shumaiErr = NewShumaiError(codeStr, msg)
}
if s.logger != nil {
errorResponse := map[string]interface{}{
"msg": msg,
}
s.logger.LogError(requestID, transactionID, apiPath, shumaiErr, errorResponse)
s.logger.LogError(requestID, transactionID, apiPath, shumaiErr, map[string]interface{}{
"request_params": reqFormData,
"response_body": string(raw),
})
}
if shumaiErr.IsNoRecord() {
return nil, errors.Join(ErrNotFound, shumaiErr)
@@ -296,14 +289,14 @@ func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqForm
dataBytes, err := json.Marshal(shumaiResp.Data)
if err != nil {
err = errors.Join(ErrSystem, fmt.Errorf("data 序列化失败: %w", err))
marshalErr := errors.Join(ErrSystem, fmt.Errorf("data 序列化失败: %w", err))
if s.logger != nil {
errorResponse := map[string]interface{}{
"msg": msg,
}
s.logger.LogError(requestID, transactionID, apiPath, err, errorResponse)
s.logger.LogError(requestID, transactionID, apiPath, marshalErr, map[string]interface{}{
"request_params": reqFormData,
"response_body": string(raw),
})
}
return nil, err
return nil, marshalErr
}
return dataBytes, nil
}