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

feat: support errors.Is for schema errors #476

Merged
merged 1 commit into from
Nov 14, 2022
Merged
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
35 changes: 35 additions & 0 deletions schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func (e ErrUnmatchable) Reasonf(format string, a ...interface{}) ErrUnmatchable
return ErrUnmatchable{e.TypeName, fmt.Errorf(format, a...)}
}

// Is provides support for Go's standard errors.Is function so that
// errors.Is(yourError, ErrUnmatchable) may be used to match the type of error.
func (e ErrUnmatchable) Is(err error) bool {
_, ok := err.(ErrUnmatchable)
return ok
}

// ErrMissingRequiredField is returned when calling 'Finish' on a NodeAssembler
// for a Struct that has not has all required fields set.
type ErrMissingRequiredField struct {
Expand All @@ -58,6 +65,13 @@ func (e ErrMissingRequiredField) Error() string {
return "missing required fields: " + strings.Join(e.Missing, ",")
}

// Is provides support for Go's standard errors.Is function so that
// errors.Is(yourError, ErrMissingRequiredField) may be used to match the type of error.
func (e ErrMissingRequiredField) Is(err error) bool {
_, ok := err.(ErrMissingRequiredField)
return ok
}

// ErrInvalidKey indicates a key is invalid for some reason.
//
// This is only possible for typed nodes; specifically, it may show up when
Expand Down Expand Up @@ -87,6 +101,13 @@ func (e ErrInvalidKey) Error() string {
}
}

// Is provides support for Go's standard errors.Is function so that
// errors.Is(yourError, ErrInvalidKey) may be used to match the type of error.
func (e ErrInvalidKey) Is(err error) bool {
_, ok := err.(ErrInvalidKey)
return ok
}

// ErrNoSuchField may be returned from lookup functions on the Node
// interface when a field is requested which doesn't exist,
// or from assigning data into on a MapAssembler for a struct
Expand All @@ -106,6 +127,13 @@ func (e ErrNoSuchField) Error() string {
return fmt.Sprintf("no such field: %s.%s", e.Type.Name(), e.Field)
}

// Is provides support for Go's standard errors.Is function so that
// errors.Is(yourError, ErrNoSuchField) may be used to match the type of error.
func (e ErrNoSuchField) Is(err error) bool {
_, ok := err.(ErrNoSuchField)
return ok
}

// ErrNotUnionStructure means data was fed into a union assembler that can't match the union.
//
// This could have one of several reasons, which are explained in the detail text:
Expand All @@ -126,3 +154,10 @@ type ErrNotUnionStructure struct {
func (e ErrNotUnionStructure) Error() string {
return fmt.Sprintf("cannot match schema: union structure constraints for %s caused rejection: %s", e.TypeName, e.Detail)
}

// Is provides support for Go's standard errors.Is function so that
// errors.Is(yourError, ErrNotUnionStructure) may be used to match the type of error.
func (e ErrNotUnionStructure) Is(err error) bool {
_, ok := err.(ErrNotUnionStructure)
return ok
}