Skip to content

Commit 08d316f

Browse files
committed
Add sentinel errors for slice operations
1 parent 9cddc48 commit 08d316f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pkg/utils/slice_utils.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package utils
22

33
import (
4+
"errors"
45
"fmt"
56
"strings"
7+
)
68

7-
"github.com/pkg/errors"
9+
// Package-level sentinel errors for slice operations
10+
var (
11+
ErrNilInput = errors.New("input must not be nil")
12+
ErrNonStringElement = errors.New("element is not a string")
813
)
914

1015
// SliceContainsString checks if a string is present in a slice
@@ -68,14 +73,14 @@ func SliceOfInterfacesToSliceOfStrings(input []any) []string {
6873
// This function returns an error if any element is not a string.
6974
func SliceOfInterfacesToSliceOfStringsWithTypeAssertion(input []any) ([]string, error) {
7075
if input == nil {
71-
return nil, errors.New("input must not be nil")
76+
return nil, ErrNilInput
7277
}
7378

7479
output := make([]string, len(input))
7580
for i, current := range input {
7681
s, ok := current.(string)
7782
if !ok {
78-
return nil, errors.New(fmt.Sprintf("element at index %d is not a string, got %T", i, current))
83+
return nil, fmt.Errorf("%w: index=%d, got=%T", ErrNonStringElement, i, current)
7984
}
8085
output[i] = s
8186
}

0 commit comments

Comments
 (0)