store: new error type (noElementsLeft)

This commit is contained in:
Benedict 2022-03-30 14:25:44 +02:00 committed by Benedict Schlüter
parent 0718452bf9
commit 04be09d5d3
3 changed files with 16 additions and 6 deletions

View File

@ -243,10 +243,10 @@ type EtcdIterator struct {
keys []string
}
// GetNext gets the next element.
// GetNext returns the next element of the iterator.
func (i *EtcdIterator) GetNext() (string, error) {
if i.idx >= len(i.keys) {
return "", fmt.Errorf("index out of range [%d] with length %d", i.idx, len(i.keys))
return "", &NoElementsLeftError{idx: i.idx}
}
key := i.keys[i.idx]
i.idx++

View File

@ -1,7 +1,6 @@
package store
import (
"fmt"
"strings"
"sync"
)
@ -170,11 +169,11 @@ type StdIterator struct {
// GetNext returns the next element of the iterator.
func (i *StdIterator) GetNext() (string, error) {
if i.idx >= len(i.keys) {
return "", fmt.Errorf("index out of range [%d] with length %d", i.idx, len(i.keys))
return "", &NoElementsLeftError{idx: i.idx}
}
val := i.keys[i.idx]
key := i.keys[i.idx]
i.idx++
return val, nil
return key, nil
}
// HasNext returns true if there are elements left to get with GetNext().

View File

@ -53,3 +53,14 @@ type ValueUnsetError struct {
func (s *ValueUnsetError) Error() string {
return fmt.Sprintf("store: requested value not set: %s", s.requestedValue)
}
// NoElementsLeftError occurs when trying to get an element from an interator that
// doesn't have elements left.
type NoElementsLeftError struct {
idx int
}
// Error implements the Error interface.
func (n *NoElementsLeftError) Error() string {
return fmt.Sprintf("index out of range [%d]", n.idx)
}