From 5aaa644fc5b7a4aec8e54042db506514b5437b86 Mon Sep 17 00:00:00 2001 From: yyasha Date: Sat, 20 Jan 2024 21:49:14 +0300 Subject: [PATCH] fix errors --- internal/parser/parser.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 635d38f..df5ca23 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -22,6 +22,13 @@ var client *httpclient.Client = httpclient.New().WithBaseURL(baseUrl) var PHPSESSID string var parseKey string +var ( + KeyNotFoundErr = errors.New("key not found") + CookieNotFoundErr = errors.New("cookie not found") + NotSuccessReqErr = errors.New("not success request") + EmptyLinkErr = errors.New("empty link") +) + // SaveAllPages saves all pages to storage. func SaveAllPages(log slog.Logger) error { const op = "parser.SaveAllPages" @@ -69,8 +76,7 @@ func SavePage(log slog.Logger, page int) (int, error) { continue } if !resp.Success { - err = errors.New("not success request") - return 0, fmt.Errorf("%s: %w", op, err) + return 0, fmt.Errorf("%s: %w", op, NotSuccessReqErr) } body = strings.NewReader(resp.Data.Results) break @@ -113,7 +119,7 @@ func GetRecipe(r *models.Recipe) error { const op = "parser.GetRecipe" if r.Link == "" { - return fmt.Errorf("%s: %w", op, errors.New("empty link")) + return fmt.Errorf("%s: %w", op, EmptyLinkErr) } // send request body, err := client. @@ -167,11 +173,10 @@ func GetRecipe(r *models.Recipe) error { var advices []string s.Find("div.recipe-footer__additional-text>p").Each(func(i int, q *goquery.Selection) { if q.Find("br").Length() > 0 { - html, err := q.Html() - if err != nil { - - return fmt.Errorf("%s: %w", op, err) - } + html, _ := q.Html() + // if err != nil { + // return fmt.Errorf("%s: %w", op, err) + // } advice_list := q.SetHtml(strings.Replace(html, "
", "\n", -1)).Text() advices_arr := strings.Split(advice_list, "\n") var advices_arr_res []string = make([]string, 0, len(advices_arr)) @@ -272,9 +277,8 @@ func GetKey(log slog.Logger) error { log.Debug("New KEY =", parseKey) return nil } - err = errors.New("key not found") - return fmt.Errorf("%s: %w", op, err) + return fmt.Errorf("%s: %w", op, KeyNotFoundErr) } func GetPHPSESSID(log slog.Logger) error { @@ -301,7 +305,6 @@ func GetPHPSESSID(log slog.Logger) error { return nil } } - err = errors.New("cookie not found") - return fmt.Errorf("%s: %w", op, err) + return fmt.Errorf("%s: %w", op, CookieNotFoundErr) }