From 270d554b95cd28080ab1160a658b3245a53dcb62 Mon Sep 17 00:00:00 2001 From: Kirill Zhuravlev Date: Wed, 15 Feb 2023 04:02:23 +0100 Subject: [PATCH] handle error in extractCategories --- main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index c02b308a..8c094c71 100644 --- a/main.go +++ b/main.go @@ -175,7 +175,6 @@ func renderCategories(categories map[string]Category) error { } func renderSitemap(categories map[string]Category) error { - // FIXME: handle error f, err := os.Create(outSitemapFile) if err != nil { return fmt.Errorf("create sitemap file `%s`: %w", outSitemapFile, err) @@ -192,29 +191,43 @@ func renderSitemap(categories map[string]Category) error { func extractCategories(doc *goquery.Document) (map[string]Category, error) { categories := make(map[string]Category) + var rootErr error + doc. Find("body #contents"). NextFiltered("ul"). Find("ul"). - Each(func(_ int, selUl *goquery.Selection) { + EachWithBreak(func(_ int, selUl *goquery.Selection) bool { + if rootErr != nil { + return false + } + selUl. Find("li a"). - Each(func(_ int, s *goquery.Selection) { + EachWithBreak(func(_ int, s *goquery.Selection) bool { selector, exists := s.Attr("href") if !exists { - return + return true } category, err := extractCategory(doc, selector) if err != nil { - return + rootErr = fmt.Errorf("extract category: %w", err) + return false } categories[selector] = *category + + return true }) + + return true }) - // FIXME: handle error + if rootErr != nil { + return nil, fmt.Errorf("extract categories: %w", rootErr) + } + return categories, nil }