Skip to content

Commit

Permalink
Introduce error util ErrInvalidCombination for invalid combination (#638
Browse files Browse the repository at this point in the history
)

* Introduce error util ErrInvalidCombination for invalid combination

Sometimes valid value becomes invalid value by combination.

example 1. knative/serving#5382
example 2. following combination in `spec.traffic`.

```
  traffic:
  - latestRevision: true
    revisionName: hello-example-dk7nd
    percent: 100
```

But there are no error util for them, so we need to create
custom error like knative/serving@c1583f3
or `ErrInvalidValue`.

The custom error will make code complicated and `ErrInvalidValue` is
not debug friendly.

To solve it, this patch introduces an util func `ErrInvalidCombination`.

* Introduce ErrGeneric instead of ErrInvalidCombination
  • Loading branch information
nak3 authored and knative-prow-robot committed Sep 12, 2019
1 parent 2e2ab7a commit 3415797
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions apis/field_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,29 @@ func ErrDisallowedUpdateDeprecatedFields(fieldPaths ...string) *FieldError {
}

// ErrInvalidArrayValue constructs a FieldError for a repetetive `field`
// at `index` that has received an invalid string value.
// at `index` that has received an invalid value.
func ErrInvalidArrayValue(value interface{}, field string, index int) *FieldError {
return ErrInvalidValue(value, CurrentField).ViaFieldIndex(field, index)
}

// ErrInvalidValue constructs a FieldError for a field that has received an
// invalid string value.
// invalid value.
func ErrInvalidValue(value interface{}, fieldPath string) *FieldError {
return &FieldError{
Message: fmt.Sprintf("invalid value: %v", value),
Paths: []string{fieldPath},
}
}

// ErrGeneric constructs a FieldError to allow for the different error strings for the
// the different cases.
func ErrGeneric(diagnostic string, fieldPaths ...string) *FieldError {
return &FieldError{
Message: diagnostic,
Paths: fieldPaths,
}
}

// ErrMissingOneOf is a variadic helper method for constructing a FieldError for
// not having at least one field in a mutually exclusive field group.
func ErrMissingOneOf(fieldPaths ...string) *FieldError {
Expand Down
5 changes: 5 additions & 0 deletions apis/field_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ Body.`,
err: ErrInvalidValue(5*time.Second, "bar"),
prefixes: [][]string{{"baz"}},
want: `invalid value: 5s: baz.bar`,
}, {
name: "invalid value propagation",
err: ErrGeneric("this is a generic error", "foo", "bar"),
prefixes: [][]string{{"baz"}},
want: `this is a generic error: baz.bar, baz.foo`,
}, {
name: "missing mutually exclusive fields",
err: ErrMissingOneOf("foo", "bar"),
Expand Down

0 comments on commit 3415797

Please sign in to comment.