Skip to content
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: 12 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func (s *StrictMissingError) String() string {
return buf.String()
}

// Unwrap returns wrapped decode errors
//
// Implements errors.Join() interface.
func (s *StrictMissingError) Unwrap() []error {
var errs []error
for i := range s.Errors {
errs = append(errs, &s.Errors[i])
}
return errs
}

type Key []string

// Error returns the error message contained in the DecodeError.
Expand All @@ -78,7 +89,7 @@ func (e *DecodeError) Key() Key {
return e.key
}

// decodeErrorFromHighlight creates a DecodeError referencing a highlighted
// wrapDecodeError creates a DecodeError referencing a highlighted
// range of bytes from document.
//
// highlight needs to be a sub-slice of document, or this function panics.
Expand Down
15 changes: 15 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ func TestDecodeError_Accessors(t *testing.T) {
assert.Equal(t, "bar", e.String())
}

func TestStrictErrorUnwrap(t *testing.T) {
fo := bytes.NewBufferString(`
Missing = 1
OtherMissing = 1
`)
var out struct{}
err := NewDecoder(fo).DisallowUnknownFields().Decode(&out)
assert.Error(t, err)

strictErr := &StrictMissingError{}
assert.True(t, errors.As(err, &strictErr))

assert.Equal(t, 2, len(strictErr.Unwrap()))
}

func ExampleDecodeError() {
doc := `name = 123__456`

Expand Down