rpc: return err if page is incorrect (less than 0 or greater than tot… (#3825)

* rpc: return err if page is incorrect (less than 0 or greater than total pages)

Fixes #3813

* fix rpc_test
This commit is contained in:
Anton Kaliaev
2019-07-23 12:25:59 +04:00
committed by Jack Zampolin
parent df6df61ea9
commit e89991c445
4 changed files with 43 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
package core
import (
"fmt"
"time"
cfg "github.com/tendermint/tendermint/config"
@@ -145,19 +146,24 @@ func SetConfig(c cfg.RPCConfig) {
config = c
}
func validatePage(page, perPage, totalCount int) int {
func validatePage(page, perPage, totalCount int) (int, error) {
if perPage < 1 {
return 1
panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
}
if page == 0 {
return 1, nil // default
}
pages := ((totalCount - 1) / perPage) + 1
if page < 1 {
page = 1
} else if page > pages {
page = pages
if pages == 0 {
pages = 1 // one page (even if it's empty)
}
if page < 0 || page > pages {
return 1, fmt.Errorf("page should be within [0, %d] range, given %d", pages, page)
}
return page
return page, nil
}
func validatePerPage(perPage int) int {