Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yrobla committed Feb 19, 2024
1 parent eb5ba88 commit d5a7663
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/feeds/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package maven
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -72,6 +73,18 @@ func (feed Feed) fetchPackages(page int) ([]Package, error) {
}
defer resp.Body.Close()

// Handle rate limiting (HTTP status code 429).
if resp.StatusCode == http.StatusTooManyRequests {
time.Sleep(5 * time.Second)
return feed.fetchPackages(page) // Retry the request
}

// Handle other HTTP status codes
if resp.StatusCode != http.StatusOK {
newErr := errors.New("unexpected HTTP status code")

Check failure on line 84 in pkg/feeds/maven/maven.go

View workflow job for this annotation

GitHub Actions / build-verify

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(\"unexpected HTTP status code\")" (goerr113)

Check failure on line 84 in pkg/feeds/maven/maven.go

View workflow job for this annotation

GitHub Actions / build-verify

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(\"unexpected HTTP status code\")" (goerr113)
return nil, fmt.Errorf("%w", newErr)
}

// Decode response.
var response Response
err = json.NewDecoder(resp.Body).Decode(&response)
Expand Down

0 comments on commit d5a7663

Please sign in to comment.