feat: SearchPodcasts 支持 context 透传并修复 resp nil 解引用
与 Charts/LoadFeed 同型:新增 SearchPodcastsContext(原方法转发 context.Background() 保持兼容);resp.StatusCode 日志移到 err 检查后, 消除网络错误/ctx 取消时的 nil 解引用 panic。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -128,8 +128,13 @@ type ChartsOptions struct {
|
||||
QueryParams map[string]string
|
||||
}
|
||||
|
||||
// SearchPodcasts 搜索播客
|
||||
// SearchPodcasts 搜索播客(无取消语义,等价于 SearchPodcastsContext(context.Background(), ...))。
|
||||
func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*model.SearchResult2, error) {
|
||||
return p.SearchPodcastsContext(context.Background(), term, options)
|
||||
}
|
||||
|
||||
// SearchPodcastsContext 搜索播客,请求挂在调用方 ctx 上:ctx 取消/超时即中断。
|
||||
func (p *ITunesProvider) SearchPodcastsContext(ctx context.Context, term string, options *SearchOptions) (*model.SearchResult2, error) {
|
||||
if term == "" {
|
||||
return nil, fmt.Errorf("搜索词不能为空")
|
||||
}
|
||||
@@ -173,7 +178,7 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m
|
||||
p.Logger.Infof("url: %s", url)
|
||||
|
||||
// 发送请求
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
@@ -184,11 +189,11 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m
|
||||
// 发送请求
|
||||
p.Limiter.Take()
|
||||
resp, err := p.Client.Do(req)
|
||||
p.Logger.Infof("response code: %d", resp.StatusCode)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("发送请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
p.Logger.Infof("response code: %d", resp.StatusCode)
|
||||
|
||||
// 检查响应状态
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
||||
Reference in New Issue
Block a user