fix: handle empty data set

This commit is contained in:
Reinaldy Rafli 2021-07-17 17:34:02 +07:00
parent a60c7b91cc
commit 19ca25eb5b
3 changed files with 8 additions and 2 deletions

View File

@ -45,7 +45,12 @@ func GetRandomJokeFromCache(memory *cache.Cache) (string, error) {
return "", nil return "", nil
} }
random := rand.Intn(len(data)) dataLength := len(data)
if dataLength == 0 {
return "", models.ErrEmpty
}
random := rand.Intn(dataLength)
joke := data[random].Link joke := data[random].Link
return joke, nil return joke, nil

View File

@ -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 ErrTxDone = errors.New("transaction has already been committed or rolled back")
var ErrNotFound = errors.New("record not found") var ErrNotFound = errors.New("record not found")
var ErrEmpty = errors.New("record is empty")