From c8fec239645869061ab61f5dbe398ca09ebdf1fe Mon Sep 17 00:00:00 2001 From: Abner Coimbre Date: Sun, 25 Jan 2026 11:35:42 -0800 Subject: [PATCH] bloggify() should return error messages --- main.go | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 45d5749..1bb7b1b 100644 --- a/main.go +++ b/main.go @@ -192,9 +192,9 @@ func sendTest(cfg *Config, markdownFile string) { /* Convert Markdown to Blog Post */ /* *** */ - url, valid := bloggify(cfg, subject, body, false) - if !valid { - fmt.Printf("Error converting '%s' into a blog post\n", markdownFile) + url, err := bloggify(cfg, subject, body, false) + if err != nil { + fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err) return } @@ -218,9 +218,9 @@ func sendTest(cfg *Config, markdownFile string) { os.WriteFile(sha1File, []byte(sumString), 0666) } -func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid bool) { +func bloggify(cfg *Config, subject, body string, isLive bool) (string, error) { if !isLive { - return "https://handmadecities.com/meetup-invite-test", true + return "https://handmadecities.com/meetup-invite-test", nil } /* Prepare request payload */ @@ -232,16 +232,14 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid payloadBytes, err := json.Marshal(payload) if err != nil { - fmt.Printf("Error encoding bloggify payload: %v\n", err) - return "", false + return "", fmt.Errorf("error encoding payload: %v", err) } /* Prepare request */ /* *** */ req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes)) if err != nil { - fmt.Printf("Error creating bloggify request: %v\n", err) - return "", false + return "", fmt.Errorf("error creating request: %v", err) } req.Header.Set("Content-Type", "application/json") @@ -252,24 +250,21 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid client := http.Client{} resp, err := client.Do(req) if err != nil { - fmt.Printf("Error making bloggify request: %v\n", err) - return "", false + return "", fmt.Errorf("error making request: %v", err) } defer resp.Body.Close() /* Fail if status code is not in the 2xx range */ /* *** */ if resp.StatusCode < 200 || resp.StatusCode >= 300 { - fmt.Printf("Bloggify error: HTTP status code %d\n", resp.StatusCode) - return "", false + return "", fmt.Errorf("error: HTTP status code %d", resp.StatusCode) } /* Read response's body */ /* *** */ respBody, err := ioutil.ReadAll(resp.Body) if err != nil { - fmt.Printf("Error reading bloggify response body: %v\n", err) - return "", false + return "", fmt.Errorf("error reading response body: %v", err) } /* Parse JSON response */ @@ -277,18 +272,17 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid var data map[string]interface{} if err := json.Unmarshal(respBody, &data); err != nil { fmt.Printf("Error unmarshalling bloggify JSON: %v\n", err) - return "", false + return "", fmt.Errorf("error unmarshalling JSON: %v", err) } /* Extract the URL from the response */ /* *** */ url, ok := data["url"].(string) if !ok || url == "" { - fmt.Println("Bloggify response format error: 'url' field not found or empty") - return "", false + return "", fmt.Errorf("response format error: 'url' field not found or empty") } - return url, true + return url, nil } func sendNews(cfg *Config, markdownFile string) { @@ -313,9 +307,9 @@ func sendNews(cfg *Config, markdownFile string) { /* Convert Markdown to Blog Post */ /* *** */ - url, valid := bloggify(cfg, subject, body, true) - if !valid { - fmt.Printf("Error converting '%s' into a blog post\n", markdownFile) + url, err := bloggify(cfg, subject, body, true) + if err != nil { + fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err) return }