Add client timeout and improve error handling

- Set HTTPClientTimeout for HTTP client to prevent hanging requests
- Check for http.StatusOK instead of a generic error-range
- Use json.NewDecoder to decode error JSON for better memory usage
This commit is contained in:
Winni Neessen 2023-06-27 15:20:16 +02:00
parent 681c53c23d
commit a0b67b0367
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -49,11 +49,13 @@ type APIError struct {
// NewHTTPClient returns a new HTTP client
func NewHTTPClient(c *Config) *HTTPClient {
tc := &tls.Config{
MaxVersion: tls.VersionTLS12,
MinVersion: tls.VersionTLS12,
}
ht := http.Transport{TLSClientConfig: tc}
hc := &http.Client{Transport: &ht}
hc := &http.Client{
Timeout: HTTPClientTimeout,
Transport: &ht,
}
return &HTTPClient{c, hc}
}
@ -100,9 +102,9 @@ func (hc *HTTPClient) GetWithTimeout(u string, t time.Duration) ([]byte, error)
if err != nil {
return nil, fmt.Errorf("failed to copy HTTP response body to buffer: %w", err)
}
if sr.StatusCode >= 400 {
if sr.StatusCode != http.StatusOK {
var ae APIError
if err = json.Unmarshal(buf.Bytes(), &ae); err != nil {
if err = json.NewDecoder(buf).Decode(&ae); err != nil {
return nil, fmt.Errorf("failed to unmarshal error JSON: %w", err)
}
if ae.Code < 1 {