chore: Improve error logging for OpenSea errors / generic json unmarshalling errors (#3298)

fixes #8798
This commit is contained in:
Khushboo-dev-cpp 2023-03-20 16:53:39 +01:00 committed by GitHub
parent 6f10e8c12a
commit a3f1a84d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -186,6 +186,11 @@ func (o *Client) FetchAllCollectionsByOwner(owner common.Address) ([]OwnedCollec
return nil, err
}
// if Json is not returned there must be an error
if !json.Valid(body) {
return nil, fmt.Errorf("invalid json: %s", string(body))
}
var tmp []OwnedCollection
err = json.Unmarshal(body, &tmp)
if err != nil {
@ -277,6 +282,11 @@ func (o *Client) fetchAssets(queryParams url.Values, limit int) (*AssetContainer
return nil, err
}
// if Json is not returned there must be an error
if !json.Valid(body) {
return nil, fmt.Errorf("invalid json: %s", string(body))
}
container := AssetContainer{}
err = json.Unmarshal(body, &container)
if err != nil {

View File

@ -2,6 +2,7 @@ package opensea
import (
"encoding/json"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
@ -14,6 +15,11 @@ import (
"github.com/ethereum/go-ethereum/common"
)
const (
ExpiredKeyError = "Expired API key"
ExpectedExpiredKeyError = "invalid json: Expired API key"
)
func TestFetchAllCollectionsByOwner(t *testing.T) {
expected := []OwnedCollection{{
Collection: Collection{
@ -42,6 +48,25 @@ func TestFetchAllCollectionsByOwner(t *testing.T) {
assert.Nil(t, err)
}
func TestFetchAllCollectionsByOwnerWithInValidJson(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write([]byte(ExpiredKeyError))
if err != nil {
return
}
}))
defer srv.Close()
opensea := &Client{
client: srv.Client(),
url: srv.URL,
}
res, err := opensea.FetchAllCollectionsByOwner(common.Address{1})
assert.Nil(t, res)
assert.Equal(t, err, fmt.Errorf(ExpectedExpiredKeyError))
}
func TestFetchAllAssetsByOwnerAndCollection(t *testing.T) {
expected := AssetContainer{
Assets: []Asset{{
@ -75,3 +100,22 @@ func TestFetchAllAssetsByOwnerAndCollection(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, expected, *res)
}
func TestFetchAllAssetsByOwnerAndCollectionInvalidJson(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write([]byte(ExpiredKeyError))
if err != nil {
return
}
}))
defer srv.Close()
opensea := &Client{
client: srv.Client(),
url: srv.URL,
}
res, err := opensea.FetchAllAssetsByOwnerAndCollection(common.Address{1}, "rocky", "", 200)
assert.Nil(t, res)
assert.Equal(t, fmt.Errorf(ExpectedExpiredKeyError), err)
}