Skip to content

Commit

Permalink
utils: remove named return vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Sep 9, 2024
1 parent 33c19cb commit 94a3a69
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions internal/utils/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,20 @@ func OpenBrowser(url string) error {
}
}

func DoRequest(req *http.Request, resp interface{}) (header http.Header, statusCode int, err error) {
func DoRequest(req *http.Request, resp interface{}) (http.Header, int, error) {
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
defer r.Body.Close()
defer io.Copy(io.Discard, r.Body)

header = r.Header
statusCode = r.StatusCode

if statusCode < 200 || statusCode >= 300 {
if r.StatusCode < 200 || r.StatusCode >= 300 {
lr := io.LimitReader(r.Body, 1<<20) // 1MiB
errMsg, _ := io.ReadAll(lr)
err = fmt.Errorf("HTTP error: %s (status: %d)", string(errMsg), statusCode)
return http.Header{}, 0, fmt.Errorf("HTTP error: %s (status: %d)", string(errMsg), r.StatusCode)
} else if resp != nil {
err = json.NewDecoder(r.Body).Decode(resp)
return http.Header{}, 0, json.NewDecoder(r.Body).Decode(resp)
}

return
return r.Header, r.StatusCode, nil
}

0 comments on commit 94a3a69

Please sign in to comment.