fix: handle empty data set
This commit is contained in:
parent
a60c7b91cc
commit
19ca25eb5b
|
@ -45,7 +45,12 @@ func GetRandomJokeFromCache(memory *cache.Cache) (string, error) {
|
|||
return "", nil
|
||||
}
|
||||
|
||||
random := rand.Intn(len(data))
|
||||
dataLength := len(data)
|
||||
if dataLength == 0 {
|
||||
return "", models.ErrEmpty
|
||||
}
|
||||
|
||||
random := rand.Intn(dataLength)
|
||||
joke := data[random].Link
|
||||
|
||||
return joke, nil
|
||||
|
|
|
@ -7,3 +7,4 @@ var ErrConnDone = errors.New("connection is already closed")
|
|||
var ErrTxDone = errors.New("transaction has already been committed or rolled back")
|
||||
|
||||
var ErrNotFound = errors.New("record not found")
|
||||
var ErrEmpty = errors.New("record is empty")
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/aldy505/bob"
|
||||
)
|
||||
|
||||
// Set up the table connection, create table if not exists
|
||||
// Setup the table connection, create table if not exists
|
||||
func Setup() error {
|
||||
db := New()
|
||||
|
||||
|
|
Loading…
Reference in New Issue