From 8c1abd160dfec18e626b33781aee51388e4499fb Mon Sep 17 00:00:00 2001 From: Mehrdad Date: Fri, 29 Sep 2023 13:01:56 +0330 Subject: [PATCH 1/4] Add returned http code checking and error messages enhancement --- wbmalert.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/wbmalert.go b/wbmalert.go index e948adc..c2335f8 100644 --- a/wbmalert.go +++ b/wbmalert.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "log" "net/http" @@ -66,26 +67,30 @@ func createInitialSnapshot(website *website, wg *sync.WaitGroup) { content, err := getWebsiteAsString(website) if err == nil { website.Snapshot = content - log.Println("Created initial snapshot for " + website.Name) + log.Println("[INFO]", "Created initial snapshot for "+website.Name) } else { - log.Println(err) + log.Println("[ERROR]", website.Name, err) } defer wg.Done() } func getWebsiteAsString(website *website) (string, error) { request, err := http.NewRequest(http.MethodGet, website.Url, nil) + errorText := fmt.Sprintf("Error fetching %s", website.Name) if err != nil { - return "Error", err + return errorText, err } resp, err := client.Do(request) if err != nil { - return "Error", err + return errorText, err + } + if resp.StatusCode != 200 { + return errorText, fmt.Errorf("http status code error occured : %d", resp.StatusCode) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return "Error", err + return errorText, err } content := string(body[:]) content = removeByRegexp(content, website.RegexpRemove) @@ -102,22 +107,22 @@ func checkWebsite(website *website, wg *sync.WaitGroup) { printContentChangeMsg(website) playSound() } else { - log.Println("No changes for " + website.Name) + log.Println("[INFO]", "No changes for "+website.Name) } } else { - log.Println(err) + log.Println("[ERROR]", website.Name, err) } defer wg.Done() } func printContentChangeMsg(website *website) { - log.Println("========= " + website.Name + " =========") - log.Println("Content changed: " + website.Url) - log.Println("====================" + strings.Repeat("=", len(website.Name))) + log.Println("[INFO]", "========= "+website.Name+" =========") + log.Println("[INFO]", "Content changed: "+website.Url) + log.Println("[INFO]", "===================="+strings.Repeat("=", len(website.Name))) } func goToSleep() { - log.Printf("Going to sleep for %d seconds", interval) + log.Printf("[INFO] Going to sleep for %d seconds", interval) time.Sleep(time.Duration(interval) * time.Second) } From c6bc840574e5c764321adea533b514ab78e15729 Mon Sep 17 00:00:00 2001 From: Mehrdad Date: Sun, 1 Oct 2023 05:14:54 +0330 Subject: [PATCH 2/4] Fix: test supports http codes --- wbmalert_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wbmalert_test.go b/wbmalert_test.go index 83937da..bd5e4a7 100644 --- a/wbmalert_test.go +++ b/wbmalert_test.go @@ -20,7 +20,8 @@ func (m *mockClient) Do(req *http.Request) (*http.Response, error) { reader := strings.NewReader(HTTP_GET_BODY) readCloser := io.NopCloser(reader) response := http.Response{ - Body: readCloser, + Body: readCloser, + StatusCode: 200, } return &response, nil } From 8c2d3c71bcc9cf3636c06a735a2d0ea0a225a276 Mon Sep 17 00:00:00 2001 From: Mehrdad Date: Sun, 1 Oct 2023 16:17:50 +0330 Subject: [PATCH 3/4] fix: get result good status code criterion --- wbmalert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wbmalert.go b/wbmalert.go index c2335f8..5104e24 100644 --- a/wbmalert.go +++ b/wbmalert.go @@ -84,7 +84,7 @@ func getWebsiteAsString(website *website) (string, error) { if err != nil { return errorText, err } - if resp.StatusCode != 200 { + if resp.StatusCode >= 400 { return errorText, fmt.Errorf("http status code error occured : %d", resp.StatusCode) } defer resp.Body.Close() From 3e1a93a177f70c06c0d97513f786a4e9dd6597f4 Mon Sep 17 00:00:00 2001 From: Mehrdad Date: Sun, 1 Oct 2023 16:45:19 +0330 Subject: [PATCH 4/4] Add slog as logger --- wbmalert.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/wbmalert.go b/wbmalert.go index 5104e24..e41d9a2 100644 --- a/wbmalert.go +++ b/wbmalert.go @@ -3,7 +3,7 @@ package main import ( "fmt" "io" - "log" + "log/slog" "net/http" "strings" "sync" @@ -67,9 +67,9 @@ func createInitialSnapshot(website *website, wg *sync.WaitGroup) { content, err := getWebsiteAsString(website) if err == nil { website.Snapshot = content - log.Println("[INFO]", "Created initial snapshot for "+website.Name) + slog.Info("Created initial snapshot for", "website", website.Name) } else { - log.Println("[ERROR]", website.Name, err) + slog.Error("Can't create snapshot for", "website", website.Name, "error", err) } defer wg.Done() } @@ -107,22 +107,22 @@ func checkWebsite(website *website, wg *sync.WaitGroup) { printContentChangeMsg(website) playSound() } else { - log.Println("[INFO]", "No changes for "+website.Name) + slog.Info("No changes for", "website", website.Name) } } else { - log.Println("[ERROR]", website.Name, err) + slog.Error("website", website.Name, "error", err) } defer wg.Done() } func printContentChangeMsg(website *website) { - log.Println("[INFO]", "========= "+website.Name+" =========") - log.Println("[INFO]", "Content changed: "+website.Url) - log.Println("[INFO]", "===================="+strings.Repeat("=", len(website.Name))) + slog.Info("========= " + website.Name + " =========") + slog.Info("Content changed: " + website.Url) + slog.Info("====================" + strings.Repeat("=", len(website.Name))) } func goToSleep() { - log.Printf("[INFO] Going to sleep for %d seconds", interval) + slog.Info("Going to sleep for", "seconds", interval) time.Sleep(time.Duration(interval) * time.Second) }