Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/libs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ The `pkg/errors` package contains the `ReasonableError` type, which combines a n
- `Errorf(...)` can be used to wrap an existing `ReasonableError` together with a new error, similarly to how `fmt.Errorf(...)` does it for standard errors.
- `NewReasonableErrorList(...)` or `Join(...)` can be used to work with lists of errors. `Aggregate()` turns them into a single error again.

### Ignore Invalid User Input

`ErrInvalidUserInput` can be used to create errors that can be ignored with `IgnoreInvalidUserInput(...)` at the end of reconciliation. This allows you to handle errors properly and skip unnecessary requeues.
16 changes: 16 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package errors

import (
"errors"
)

// ErrInvalidUserInput indicates invalid user input.
var ErrInvalidUserInput = errors.New("invalid user input")

// IgnoreUserInputError returns nil on invalid user input.
func IgnoreInvalidUserInput(err error) error {
if errors.Is(err, ErrInvalidUserInput) {
return nil
}
return err
}
53 changes: 53 additions & 0 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package errors_test

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"

ctrlutils "github.com/openmcp-project/controller-utils/pkg/errors"
)

func TestIgnoreInvalidUserInput(t *testing.T) {
tests := []struct {
name string // description of this test case
// Named input parameters for target function.
err error
wrappedError error
wantErr bool
}{
{
name: "user input error is ignored",
err: fmt.Errorf("value out of range %w", ctrlutils.ErrInvalidUserInput),
wantErr: false,
},
{
name: "regular error is returned",
err: errors.New("regular error"),
wantErr: true,
},
{
name: "nil is nil",
err: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotErr := ctrlutils.IgnoreInvalidUserInput(tt.err)
if gotErr != nil {
if !tt.wantErr {
t.Errorf("IgnoreInvalidUserInput() failed: %v", gotErr)
}
assert.Equal(t, tt.err, gotErr)
if tt.wrappedError != nil {
assert.ErrorIs(t, tt.err, tt.wrappedError)
}
return
}
assert.False(t, tt.wantErr)
})
}
}
Loading