Merge pull request #440 from hermeznetwork/fix/api-empty-arrays empty array instead of 404
@@ -35,6 +35,9 @@ PendingItems uint64 `json:"pendingItems"`
} func (t testBatchesResponse) GetPending() (pendingItems, lastItemID uint64) { + if len(t.Batches) == 0 { + return 0, 0 + } pendingItems = t.PendingItems lastItemID = t.Batches[len(t.Batches)-1].ItemID return pendingItems, lastItemID@@ -220,6 +223,13 @@ }
} assertBatches(t, minMaxBatchNumBatches, fetchedBatches) + // Empty array + fetchedBatches = []testBatch{} + path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25) + err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter) + assert.NoError(t, err) + assertBatches(t, []testBatch{}, fetchedBatches) + // 400 // Invalid minBatchNum path = fmt.Sprintf("%s?minBatchNum=%d", endpoint, -2)@@ -228,10 +238,6 @@ assert.NoError(t, err)
// Invalid forgerAddr path = fmt.Sprintf("%s?forgerAddr=%s", endpoint, "0xG0000001") err = doBadReq("GET", path, nil, 400) - assert.NoError(t, err) - // 404 - path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25) - err = doBadReq("GET", path, nil, 404) assert.NoError(t, err) }
@@ -29,6 +29,9 @@ PendingItems uint64 `json:"pendingItems"`
} func (t testBidsResponse) GetPending() (pendingItems, lastItemID uint64) { + if len(t.Bids) == 0 { + return 0, 0 + } pendingItems = t.PendingItems lastItemID = t.Bids[len(t.Bids)-1].ItemID return pendingItems, lastItemID@@ -127,6 +130,13 @@ }
} assertBids(t, slotNumBidderAddrBids, fetchedBids) + // Empty array + fetchedBids = []testBid{} + path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, 5, tc.bids[1].Bidder.String()) + err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter) + assert.NoError(t, err) + assertBids(t, []testBid{}, fetchedBids) + // 400 // No filters path = fmt.Sprintf("%s?limit=%d", endpoint, limit)@@ -139,10 +149,6 @@ assert.NoError(t, err)
// Invalid bidderAddress path = fmt.Sprintf("%s?bidderAddr=%s", endpoint, "0xG0000001") err = doBadReq("GET", path, nil, 400) - assert.NoError(t, err) - // 404 - path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, 5, tc.bids[1].Bidder.String()) - err = doBadReq("GET", path, nil, 404) assert.NoError(t, err) }
@@ -16,6 +16,9 @@ PendingItems uint64 `json:"pendingItems"`
} func (t testCoordinatorsResponse) GetPending() (pendingItems, lastItemID uint64) { + if len(t.Coordinators) == 0 { + return 0, 0 + } pendingItems = t.PendingItems lastItemID = t.Coordinators[len(t.Coordinators)-1].ItemID return pendingItems, lastItemID@@ -67,7 +70,6 @@ for i := 0; i < len(tc.coordinators); i++ {
reversedCoordinators = append(reversedCoordinators, tc.coordinators[len(tc.coordinators)-1-i]) } assertCoordinators(t, reversedCoordinators, fetchedCoordinators) - for _, filteredCoord := range tc.coordinators { // By bidder fetchedCoordinators = []historydb.CoordinatorAPI{}@@ -87,13 +89,16 @@ assert.NoError(t, err)
assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators) } + // Empty array + fetchedCoordinators = []historydb.CoordinatorAPI{} + path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint) + err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter) + assert.NoError(t, err) + assertCoordinators(t, []historydb.CoordinatorAPI{}, fetchedCoordinators) + // 400 path = fmt.Sprintf("%s?bidderAddr=0x001", endpoint) err = doBadReq("GET", path, nil, 400) - assert.NoError(t, err) - // 404 - path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint) - err = doBadReq("GET", path, nil, 404) assert.NoError(t, err) }
@@ -43,6 +43,9 @@ PendingItems uint64 `json:"pendingItems"`
} func (t testExitsResponse) GetPending() (pendingItems, lastItemID uint64) { + if len(t.Exits) == 0 { + return 0, 0 + } pendingItems = t.PendingItems lastItemID = t.Exits[len(t.Exits)-1].ItemID return pendingItems, lastItemID@@ -248,6 +251,12 @@ path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
err = doGoodReqPaginated(path, historydb.OrderDesc, &testExitsResponse{}, appendIter) assert.NoError(t, err) assertExitAPIs(t, flipedExits, fetchedExits) + // Empty array + fetchedExits = []testExit{} + path = fmt.Sprintf("%s?batchNum=999999", endpoint) + err = doGoodReqPaginated(path, historydb.OrderDesc, &testExitsResponse{}, appendIter) + assert.NoError(t, err) + assertExitAPIs(t, []testExit{}, fetchedExits) // 400 path = fmt.Sprintf( "%s?accountIndex=%s&hezEthereumAddress=%s",@@ -257,13 +266,6 @@ err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err) path = fmt.Sprintf("%s?tokenId=X", endpoint) err = doBadReq("GET", path, nil, 400) - assert.NoError(t, err) - // 404 - path = fmt.Sprintf("%s?batchNum=999999", endpoint) - err = doBadReq("GET", path, nil, 404) - assert.NoError(t, err) - path = fmt.Sprintf("%s?fromItem=1000999999", endpoint) - err = doBadReq("GET", path, nil, 404) assert.NoError(t, err) }
@@ -215,12 +215,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -347,12 +341,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -565,12 +553,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -687,12 +669,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -850,12 +826,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -960,12 +930,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:@@ -1085,12 +1049,6 @@ content:
application/json: schema: $ref: '#/components/schemas/Error400' - '404': - description: Not found. - content: - application/json: - schema: - $ref: '#/components/schemas/Error404' '500': description: Internal server error. content:
@@ -88,6 +88,9 @@ PendingItems uint64 `json:"pendingItems"`
} func (t testTxsResponse) GetPending() (pendingItems, lastItemID uint64) { + if len(t.Txs) == 0 { + return 0, 0 + } pendingItems = t.PendingItems lastItemID = t.Txs[len(t.Txs)-1].ItemID return pendingItems, lastItemID@@ -420,6 +423,12 @@ for i := 0; i < len(tc.txs); i++ {
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i]) } assertTxs(t, flipedTxs, fetchedTxs) + // Empty array + fetchedTxs = []testTx{} + path = fmt.Sprintf("%s?batchNum=999999", endpoint) + err = doGoodReqPaginated(path, historydb.OrderDesc, &testTxsResponse{}, appendIter) + assert.NoError(t, err) + assertTxs(t, []testTx{}, fetchedTxs) // 400 path = fmt.Sprintf( "%s?accountIndex=%s&hezEthereumAddress=%s",@@ -429,13 +438,6 @@ err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err) path = fmt.Sprintf("%s?tokenId=X", endpoint) err = doBadReq("GET", path, nil, 400) - assert.NoError(t, err) - // 404 - path = fmt.Sprintf("%s?batchNum=999999", endpoint) - err = doBadReq("GET", path, nil, 404) - assert.NoError(t, err) - path = fmt.Sprintf("%s?fromItem=1000999999", endpoint) - err = doBadReq("GET", path, nil, 404) assert.NoError(t, err) }
@@ -1,7 +1,6 @@
package historydb import ( - "database/sql" "errors" "fmt" "math"@@ -265,7 +264,7 @@ return nil, 0, tracerr.Wrap(err)
} batches := db.SlicePtrsToSlice(batchPtrs).([]BatchAPI) if len(batches) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return batches, 0, nil } return batches, batches[0].TotalItems - uint64(len(batches)), nil }@@ -441,7 +440,7 @@ }
// log.Debug(query) bids := db.SlicePtrsToSlice(bidPtrs).([]BidAPI) if len(bids) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return bids, 0, nil } return bids, bids[0].TotalItems - uint64(len(bids)), nil }@@ -512,7 +511,7 @@ if err := meddler.QueryAll(hdb.db, &bids, query, argsQ...); err != nil {
return nil, 0, tracerr.Wrap(err) } if len(bids) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return []BidAPI{}, 0, nil } return db.SlicePtrsToSlice(bids).([]BidAPI), bids[0].TotalItems - uint64(len(bids)), nil }@@ -739,7 +738,7 @@ if err := meddler.QueryAll(hdb.db, &tokens, query, argsQ...); err != nil {
return nil, 0, tracerr.Wrap(err) } if len(tokens) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return []TokenWithUSD{}, 0, nil } return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), uint64(len(tokens)) - tokens[0].TotalItems, nil }@@ -1056,7 +1055,7 @@ return nil, 0, tracerr.Wrap(err)
} txs := db.SlicePtrsToSlice(txsPtrs).([]TxAPI) if len(txs) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return txs, 0, nil } return txs, txs[0].TotalItems - uint64(len(txs)), nil }@@ -1199,7 +1198,7 @@ if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
return nil, 0, tracerr.Wrap(err) } if len(exits) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return []ExitAPI{}, 0, nil } return db.SlicePtrsToSlice(exits).([]ExitAPI), exits[0].TotalItems - uint64(len(exits)), nil }@@ -1688,7 +1687,7 @@ if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
return nil, 0, tracerr.Wrap(err) } if len(coordinators) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return []CoordinatorAPI{}, 0, nil } return db.SlicePtrsToSlice(coordinators).([]CoordinatorAPI), coordinators[0].TotalItems - uint64(len(coordinators)), nil@@ -1811,7 +1810,7 @@ if err := meddler.QueryAll(hdb.db, &accounts, query, argsQ...); err != nil {
return nil, 0, tracerr.Wrap(err) } if len(accounts) == 0 { - return nil, 0, tracerr.Wrap(sql.ErrNoRows) + return []AccountAPI{}, 0, nil } return db.SlicePtrsToSlice(accounts).([]AccountAPI),