1

Add basic HTTP errors handling

This commit is contained in:
2019-09-28 20:20:23 +04:00
parent a504299150
commit a599d898b0
3 changed files with 37 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ import (
"net/url"
"os"
"strings"
"time"
)
// login function name is self-descriptive.
@@ -74,9 +75,21 @@ func obtainCookies() {
req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(formData.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
dlog.Fatal().Err(err).Msg("Не удалось отправить запрос на авторизацию в NNM-Club.")
retryCount := 0
var resp *http.Response
var err error
for {
if retryCount == 10 {
dlog.Fatal().Err(err).Msg("Не удалось отправить запрос на авторизацию в NNM-Club.")
}
resp, err = client.Do(req)
if err != nil {
dlog.Debug().Err(err).Int("попытка", retryCount).Msg("Не удалось отправить запрос, попробуем ещё раз")
retryCount++
time.Sleep(2 * time.Second)
} else {
break
}
}
defer resp.Body.Close()