Skip to content

Commit d5a7663

Browse files
committed
add error handling
1 parent eb5ba88 commit d5a7663

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/feeds/maven/maven.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package maven
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"time"
@@ -72,6 +73,18 @@ func (feed Feed) fetchPackages(page int) ([]Package, error) {
7273
}
7374
defer resp.Body.Close()
7475

76+
// Handle rate limiting (HTTP status code 429).
77+
if resp.StatusCode == http.StatusTooManyRequests {
78+
time.Sleep(5 * time.Second)
79+
return feed.fetchPackages(page) // Retry the request
80+
}
81+
82+
// Handle other HTTP status codes
83+
if resp.StatusCode != http.StatusOK {
84+
newErr := errors.New("unexpected HTTP status code")
85+
return nil, fmt.Errorf("%w", newErr)
86+
}
87+
7588
// Decode response.
7689
var response Response
7790
err = json.NewDecoder(resp.Body).Decode(&response)

0 commit comments

Comments
 (0)