registry: session.searchRepositories(): return typed error, and small cleanup

- return a errdefs.System if we fail to decode the registry's response
- use strconv.Itoa instead of fmt.Sprintf

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-10 16:13:35 +02:00
parent 68ebfa2f18
commit c5c977855d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -9,6 +9,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"sync"
@ -208,10 +209,14 @@ func (r *session) searchRepositories(term string, limit int) (*registry.SearchRe
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errdefs.Unknown(&jsonmessage.JSONError{
Message: fmt.Sprintf("Unexpected status code %d", res.StatusCode),
Message: "Unexpected status code " + strconv.Itoa(res.StatusCode),
Code: res.StatusCode,
})
}
result := new(registry.SearchResults)
return result, errors.Wrap(json.NewDecoder(res.Body).Decode(result), "error decoding registry search results")
result := &registry.SearchResults{}
err = json.NewDecoder(res.Body).Decode(result)
if err != nil {
return nil, errdefs.System(errors.Wrap(err, "error decoding registry search results"))
}
return result, nil
}