Skip to content

Commit

Permalink
slurp-url, panics on non-200, non-404 responses.
Browse files Browse the repository at this point in the history
slurp-url, panics on failing to read response body.
these changes ensure that the report will fail to generate rather than generate an incorrect report.
  • Loading branch information
lsh-0 committed Jun 5, 2023
1 parent 81ef485 commit 6a3ff35
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func slurp(path string) string {
}

// read the contents at the given `url`.
// returns an empty string on any error.
// 200 responses return the response body.
// 404 responses return an empty string.
// anything else causes a panic.
func slurp_url(url string, token string) string {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
Expand All @@ -101,18 +103,14 @@ func slurp_url(url string, token string) string {
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
if resp.StatusCode != 404 {
stderr("non-200 response from URL: %s (%d)", url, resp.StatusCode)
}
if resp.StatusCode == 404 {
return ""
}

ensure(resp.StatusCode == 200, fmt.Sprintf("non-200, non-404 response from URL: %s (%d)", url, resp.StatusCode))

body, err := io.ReadAll(resp.Body)
if err != nil {
stderr("failed to read URL contents: %s", url)
return ""
}
panicOnErr(err, "reading response body: "+url)
return string(body)
}

Expand Down

0 comments on commit 6a3ff35

Please sign in to comment.