Skip to content

Commit

Permalink
feat: support errors.Is for schema errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iand authored and rvagg committed Nov 14, 2022
1 parent 04f8cbb commit 61c9ab1
Showing 1 changed file with 35 additions and 0 deletions.
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
}

0 comments on commit 61c9ab1

Please sign in to comment.