added http status code error handling to DownloadUrl function (#2959)
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
This commit is contained in:
@@ -171,6 +171,10 @@ func IsURL(str string) bool {
|
|||||||
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
|
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 {
|
func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, localPath string) error {
|
||||||
resp, err := ctxhttp.Get(ctx, httpClient, url)
|
resp, err := ctxhttp.Get(ctx, httpClient, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -178,6 +182,10 @@ func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, local
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if !isHttp2xxSuccessful(resp.StatusCode) {
|
||||||
|
return errors.New(resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
w, err := os.Create(localPath)
|
w, err := os.Create(localPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user