added http status code error handling to DownloadUrl function (#2959)

Signed-off-by: jaynis <kranz.jannis@googlemail.com>
This commit is contained in:
jaynis
2024-06-20 18:43:46 +05:30
committed by GitHub
parent 4c4b574d07
commit 19858521fd
+8
View File
@@ -171,6 +171,10 @@ func IsURL(str string) bool {
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
}
func isHttp2xxSuccessful(status int) bool {
return status >= 200 && status < 300
}
func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, localPath string) error {
resp, err := ctxhttp.Get(ctx, httpClient, url)
if err != nil {
@@ -178,6 +182,10 @@ func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, local
}
defer resp.Body.Close()
if !isHttp2xxSuccessful(resp.StatusCode) {
return errors.New(resp.Status)
}
w, err := os.Create(localPath)
if err != nil {
return err