Skip to content

Commit

Permalink
api: hotfix 'api: paginated endpoints now return ErrAccountNotFound...'
Browse files Browse the repository at this point in the history
these filters now return an empty list (not an error)
* /accounts?accountId=foobar
* /elections?electionId=foobar
* /chain/organizations?organizationId=foobar

so the errors ErrAccountNotFound, ErrElectionNotFound, ErrOrgNotFound happen only on:
* /chain/transfers?accountId=foobar
* /chain/fees?accountId=foobar
* /votes?electionId=foobar
* /elections?organizationId=foobar
  • Loading branch information
altergui committed Aug 7, 2024
1 parent ca3f738 commit 1acfb35
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 38 deletions.
4 changes: 0 additions & 4 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,6 @@ func (a *API) accountListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext
//
// Errors returned are always of type APIerror.
func (a *API) sendAccountList(ctx *httprouter.HTTPContext, params *AccountParams) error {
if params.AccountID != "" && !a.indexer.AccountExists(params.AccountID) {
return ErrAccountNotFound
}

accounts, total, err := a.indexer.AccountList(
params.Limit,
params.Page*params.Limit,
Expand Down
4 changes: 0 additions & 4 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,6 @@ func (a *API) organizationListByFilterAndPageHandler(msg *apirest.APIdata, ctx *
//
// Errors returned are always of type APIerror.
func (a *API) sendOrganizationList(ctx *httprouter.HTTPContext, params *OrganizationParams) error {
if params.OrganizationID != "" && !a.indexer.EntityExists(params.OrganizationID) {
return ErrOrgNotFound
}

orgs, total, err := a.indexer.EntityList(
params.Limit,
params.Page*params.Limit,
Expand Down
4 changes: 0 additions & 4 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ func (a *API) electionListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
//
// Errors returned are always of type APIerror.
func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionParams) error {
if params.ElectionID != "" && !a.indexer.ProcessExists(params.ElectionID) {
return ErrElectionNotFound
}

if params.OrganizationID != "" && !a.indexer.EntityExists(params.OrganizationID) {
return ErrOrgNotFound
}
Expand Down
20 changes: 0 additions & 20 deletions test/apierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,6 @@ func TestAPIerrorWithQuery(t *testing.T) {
args: args{"GET", nil, []string{"accounts"}, "page=1234"},
want: api.ErrPageNotFound,
},
{
args: args{"GET", nil, []string{"accounts"}, "accountId=0123456789"},
want: api.ErrAccountNotFound,
},
{
args: args{"GET", nil, []string{"accounts"}, "accountId=0123456789&page=1234"},
want: api.ErrAccountNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "electionId=0123456789"},
want: api.ErrElectionNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "electionId=0123456789&page=1234"},
want: api.ErrElectionNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "organizationId=0123456789"},
want: api.ErrOrgNotFound,
Expand Down Expand Up @@ -226,10 +210,6 @@ func TestAPIerrorWithQuery(t *testing.T) {
// args: args{"GET", nil, []string{"chain", "transactions"}, "type=FOOBAR"},
// want: api.ErrParamTypeInvalid,
// },
{
args: args{"GET", nil, []string{"chain", "organizations"}, "organizationId=0123456789"},
want: api.ErrOrgNotFound,
},
{
args: args{"GET", nil, []string{"chain", "fees"}, "accountId=0123456789"},
want: api.ErrAccountNotFound,
Expand Down
7 changes: 5 additions & 2 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,12 @@ func (idx *Indexer) AccountList(limit, offset int, accountID string) ([]*indexer
return list, uint64(results[0].TotalCount), nil
}

// AccountExists returns whether the passed accountID matches at least one row in the db.
// accountID is a partial or full hex string.
// AccountExists returns whether the passed accountID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) AccountExists(accountID string) bool {
if len(accountID) != 40 {
return false
}
_, count, err := idx.AccountList(1, 0, accountID)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down
14 changes: 10 additions & 4 deletions vochain/indexer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ func (idx *Indexer) ProcessList(limit, offset int, entityID string, processID st
return list, uint64(results[0].TotalCount), nil
}

// ProcessExists returns whether the passed processID matches at least one row in the db.
// processID is a partial or full hex string.
// ProcessExists returns whether the passed processID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) ProcessExists(processID string) bool {
if len(processID) != 64 {
return false
}
_, count, err := idx.ProcessList(1, 0, "", processID, 0, 0, 0, nil, nil, nil)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down Expand Up @@ -138,9 +141,12 @@ func (idx *Indexer) EntityList(limit, offset int, entityID string) ([]indexertyp
return list, uint64(results[0].TotalCount), nil
}

// EntityExists returns whether the passed entityID matches at least one row in the db.
// entityID is a partial or full hex string.
// EntityExists returns whether the passed entityID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) EntityExists(entityID string) bool {
if len(entityID) != 40 {
return false
}
_, count, err := idx.EntityList(1, 0, entityID)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down

0 comments on commit 1acfb35

Please sign in to comment.