This commit is contained in:
2025-11-02 20:33:28 +08:00
parent bb88c78c82
commit 2773c1a60b
13 changed files with 521 additions and 132 deletions

View File

@@ -59,7 +59,7 @@ type TianYanChaResponse struct {
// NewTianYanChaService 创建天眼查服务实例
func NewTianYanChaService(baseURL, token string, timeout time.Duration) *TianYanChaService {
if timeout == 0 {
timeout = 30 * time.Second
timeout = 60 * time.Second
}
return &TianYanChaService{
@@ -112,6 +112,23 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
client := &http.Client{Timeout: t.config.Timeout}
resp, err := client.Do(req)
if err != nil {
// 检查是否是超时错误
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 {
return nil, errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", err))
}
return nil, errors.Join(ErrDatasource, fmt.Errorf("API 请求异常: %v", err))
}
defer resp.Body.Close()