Skip to content

Commit

Permalink
Fix early exit from loop on error
Browse files Browse the repository at this point in the history
  • Loading branch information
rupinr committed Oct 29, 2024
1 parent 318d865 commit 14754ee
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,32 @@ func FetchNewsTask() {

if apiErr != nil {
logger.Log.Error("Error fetching News API", "error", apiErr.Error())
return
}
defer resp.Body.Close()
var response dto.NewsdataApiResponse
body, readErr := io.ReadAll(resp.Body)

if readErr != nil {
logger.Log.Error("Error Reading response from News API", "error", readErr.Error())
return
}
continue
} else {
defer resp.Body.Close()
var response dto.NewsdataApiResponse
body, readErr := io.ReadAll(resp.Body)

for i := range response.Results {
response.Results[i].SourceUrl = getDomain(response.Results[i].SourceUrl)
}
if readErr != nil {
logger.Log.Error("Error Reading response from News API", "error", readErr.Error())
continue
}

unmarshalErr := json.Unmarshal(body, &response)
unmarshalErr := json.Unmarshal(body, &response)

if unmarshalErr != nil {
logger.Log.Error("Error Processing response from News API", "error", unmarshalErr.Error())
return
}
if unmarshalErr != nil {
logger.Log.Error("Error Processing response from News API", "error", unmarshalErr.Error())
continue
}

logger.Log.Debug("Found ariticles", "count", len(response.Results))
logger.Log.Debug("Found ariticles", "count", len(response.Results))

for _, result := range response.Results {
repository.CreateResult(result)
for idx, result := range response.Results {
logger.Log.Debug("Inserting article", "index", idx)
repository.CreateResult(result)
}
}

}
}

func getDomain(rawURL string) string {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return fmt.Sprintf("INVALID_RAW_URL_%v", rawURL)
}
return parsedURL.Host
}

func SendNewsletter() {
Expand Down

0 comments on commit 14754ee

Please sign in to comment.