From 5b3a12f00df2c15990532397a839cb8776e092be Mon Sep 17 00:00:00 2001 From: timerzz Date: Sun, 23 Mar 2025 21:27:05 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8DGet=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E7=BB=93=E6=9D=9F=E5=90=8E=EF=BC=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9Enil,nil=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/get.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/common/get.go b/common/get.go index fd1de5e..6f06d9d 100644 --- a/common/get.go +++ b/common/get.go @@ -2,6 +2,7 @@ package common import ( "errors" + "fmt" "net/http" "time" @@ -28,10 +29,12 @@ func Get(url string, options ...GetOption) (resp *http.Response, err error) { for _, option := range options { option(&getConfig) } + var req *http.Request + var get *http.Response for haveTried < retryTimes { client := &http.Client{} //client.Timeout = time.Second * 10 - req, err := http.NewRequest("GET", url, nil) + req, err = http.NewRequest("GET", url, nil) if err != nil { haveTried++ time.Sleep(retryDelay) @@ -40,13 +43,12 @@ func Get(url string, options ...GetOption) (resp *http.Response, err error) { if getConfig.userAgent != "" { req.Header.Set("User-Agent", getConfig.userAgent) } - get, err := client.Do(req) + get, err = client.Do(req) if err != nil { haveTried++ time.Sleep(retryDelay) continue } else { - if get != nil && get.ContentLength > config.Default.RequestMaxFileSize { return nil, errors.New("文件过大") } @@ -54,5 +56,5 @@ func Get(url string, options ...GetOption) (resp *http.Response, err error) { } } - return nil, err + return nil, fmt.Errorf("请求失败:%v", err) }