fix
This commit is contained in:
@@ -107,9 +107,9 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
// 将加密后的数据编码为 Base64 字符串
|
||||
content := base64.StdEncoding.EncodeToString(cipherText)
|
||||
|
||||
// 发起 HTTP 请求
|
||||
// 发起 HTTP 请求,超时时间设置为60秒
|
||||
client := &http.Client{
|
||||
Timeout: 20 * time.Second,
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", y.config.URL, strings.NewReader(content))
|
||||
if err != nil {
|
||||
@@ -125,7 +125,25 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
// 执行请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
// 检查是否是超时错误
|
||||
isTimeout := false
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
isTimeout = true
|
||||
} else if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
||||
isTimeout = true
|
||||
} else if errStr := err.Error();
|
||||
errStr == "context deadline exceeded" ||
|
||||
errStr == "timeout" ||
|
||||
errStr == "Client.Timeout exceeded" ||
|
||||
errStr == "net/http: request canceled" {
|
||||
isTimeout = true
|
||||
}
|
||||
|
||||
if isTimeout {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", err))
|
||||
} else {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
}
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user