Skip to content

Commit 61c9ab1

Browse files
iandrvagg
authored andcommitted
feat: support errors.Is for schema errors
1 parent 04f8cbb commit 61c9ab1

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎schema/errors.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func (e ErrUnmatchable) Reasonf(format string, a ...interface{}) ErrUnmatchable
4848
return ErrUnmatchable{e.TypeName, fmt.Errorf(format, a...)}
4949
}
5050

51+
// Is provides support for Go's standard errors.Is function so that
52+
// errors.Is(yourError, ErrUnmatchable) may be used to match the type of error.
53+
func (e ErrUnmatchable) Is(err error) bool {
54+
_, ok := err.(ErrUnmatchable)
55+
return ok
56+
}
57+
5158
// ErrMissingRequiredField is returned when calling 'Finish' on a NodeAssembler
5259
// for a Struct that has not has all required fields set.
5360
type ErrMissingRequiredField struct {
@@ -58,6 +65,13 @@ func (e ErrMissingRequiredField) Error() string {
5865
return "missing required fields: " + strings.Join(e.Missing, ",")
5966
}
6067

68+
// Is provides support for Go's standard errors.Is function so that
69+
// errors.Is(yourError, ErrMissingRequiredField) may be used to match the type of error.
70+
func (e ErrMissingRequiredField) Is(err error) bool {
71+
_, ok := err.(ErrMissingRequiredField)
72+
return ok
73+
}
74+
6175
// ErrInvalidKey indicates a key is invalid for some reason.
6276
//
6377
// This is only possible for typed nodes; specifically, it may show up when
@@ -87,6 +101,13 @@ func (e ErrInvalidKey) Error() string {
87101
}
88102
}
89103

104+
// Is provides support for Go's standard errors.Is function so that
105+
// errors.Is(yourError, ErrInvalidKey) may be used to match the type of error.
106+
func (e ErrInvalidKey) Is(err error) bool {
107+
_, ok := err.(ErrInvalidKey)
108+
return ok
109+
}
110+
90111
// ErrNoSuchField may be returned from lookup functions on the Node
91112
// interface when a field is requested which doesn't exist,
92113
// or from assigning data into on a MapAssembler for a struct
@@ -106,6 +127,13 @@ func (e ErrNoSuchField) Error() string {
106127
return fmt.Sprintf("no such field: %s.%s", e.Type.Name(), e.Field)
107128
}
108129

130+
// Is provides support for Go's standard errors.Is function so that
131+
// errors.Is(yourError, ErrNoSuchField) may be used to match the type of error.
132+
func (e ErrNoSuchField) Is(err error) bool {
133+
_, ok := err.(ErrNoSuchField)
134+
return ok
135+
}
136+
109137
// ErrNotUnionStructure means data was fed into a union assembler that can't match the union.
110138
//
111139
// This could have one of several reasons, which are explained in the detail text:
@@ -126,3 +154,10 @@ type ErrNotUnionStructure struct {
126154
func (e ErrNotUnionStructure) Error() string {
127155
return fmt.Sprintf("cannot match schema: union structure constraints for %s caused rejection: %s", e.TypeName, e.Detail)
128156
}
157+
158+
// Is provides support for Go's standard errors.Is function so that
159+
// errors.Is(yourError, ErrNotUnionStructure) may be used to match the type of error.
160+
func (e ErrNotUnionStructure) Is(err error) bool {
161+
_, ok := err.(ErrNotUnionStructure)
162+
return ok
163+
}

0 commit comments

Comments
 (0)