File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,17 @@ func (s *StrictMissingError) String() string {
5454 return buf .String ()
5555}
5656
57+ // Unwrap returns wrapped decode errors
58+ //
59+ // Implements errors.Join() interface.
60+ func (s * StrictMissingError ) Unwrap () []error {
61+ var errs []error
62+ for i := range s .Errors {
63+ errs = append (errs , & s .Errors [i ])
64+ }
65+ return errs
66+ }
67+
5768type Key []string
5869
5970// Error returns the error message contained in the DecodeError.
@@ -78,7 +89,7 @@ func (e *DecodeError) Key() Key {
7889 return e .key
7990}
8091
81- // decodeErrorFromHighlight creates a DecodeError referencing a highlighted
92+ // wrapDecodeError creates a DecodeError referencing a highlighted
8293// range of bytes from document.
8394//
8495// highlight needs to be a sub-slice of document, or this function panics.
Original file line number Diff line number Diff line change @@ -205,6 +205,21 @@ func TestDecodeError_Accessors(t *testing.T) {
205205 assert .Equal (t , "bar" , e .String ())
206206}
207207
208+ func TestStrictErrorUnwrap (t * testing.T ) {
209+ fo := bytes .NewBufferString (`
210+ Missing = 1
211+ OtherMissing = 1
212+ ` )
213+ var out struct {}
214+ err := NewDecoder (fo ).DisallowUnknownFields ().Decode (& out )
215+ assert .Error (t , err )
216+
217+ strictErr := & StrictMissingError {}
218+ assert .True (t , errors .As (err , & strictErr ))
219+
220+ assert .Equal (t , 2 , len (strictErr .Unwrap ()))
221+ }
222+
208223func ExampleDecodeError () {
209224 doc := `name = 123__456`
210225
You can’t perform that action at this time.
0 commit comments