func getWithStatusError()

in scan/context.go [189:222]


func getWithStatusError(url string) (resp *http.Response, err error) {
	attempt := 0
	for attempt < maxRetries {
		resp, err = http.Get(url)
		if err != nil {
			time.Sleep(util.GetExponentialBackoff(attempt))
			attempt++
			continue
		}

		if resp.StatusCode < 400 {
			return resp, nil
		}

		msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)

		// If the status code is 4xx then read the body and return an error
		// If the status code is a 5xx then check the body on the final attempt and return an error.
		if resp.StatusCode < 500 || attempt == maxRetries-1 {
			body, bodyReadErr := io.ReadAll(resp.Body)
			resp.Body.Close()

			if bodyReadErr != nil {
				return nil, errors.Wrap(bodyReadErr, fmt.Sprintf("%s: error reading body", msg))
			}
			return nil, errors.Errorf("%s: %s", msg, bytes.TrimSpace(body))
		}

		time.Sleep(util.GetExponentialBackoff(attempt))
		attempt++
	}

	return resp, err
}