Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly close HTTP responses after sending each email #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions notify_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,35 @@ func sendEmail(client *NotifyClient, email *NotifyEmail) error {
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", fmt.Sprintf("ApiKey-v1 %s", client.ApiKey))

resp, err := client.Client.Do(req)

if err != nil {
log.Error().Msgf("Error sending email: %s", err)
if err := doSendEmail(client.Client, req); err != nil {
return err
}
}

return nil
}

defer resp.Body.Close()
func doSendEmail(client *http.Client, req *http.Request) error {
resp, err := client.Do(req)
if err != nil {
log.Error().Msgf("Error sending email: %s", err)
return err
}
defer resp.Body.Close()

if resp.StatusCode != 201 {
log.Error().Msgf("Unexpected status code: %d", resp.StatusCode)
respbody, err := io.ReadAll(resp.Body)
if resp.StatusCode == 201 {
return nil
}

if err != nil {
log.Error().Msgf("Error reading response body: %s", err)
return err
}
log.Error().Msgf("Response: %s", respbody)
// A non-201 status code so craft the error.
log.Error().Msgf("Unexpected status code: %d", resp.StatusCode)
respbody, err := io.ReadAll(resp.Body)

return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if err != nil {
log.Error().Msgf("Error reading response body: %s", err)
return err
}
log.Error().Msgf("Response: %s", respbody)

return nil
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}