Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce error util ErrGeneric for invalid combination #638

Merged
merged 2 commits into from
Sep 12, 2019
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
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