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
11 changes: 11 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ func (es Errors) Error() string {
return s.String()
}

// Unwrap returns a slice of the non-nil errors in the Errors.
func (es Errors) Unwrap() []error {
errs := make([]error, 0, len(es))
for _, err := range es {
if err != nil {
errs = append(errs, err)
}
}
return errs
}

// MarshalJSON converts the Errors into a valid JSON.
func (es Errors) MarshalJSON() ([]byte, error) {
errs := map[string]interface{}{}
Expand Down
13 changes: 13 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func TestErrors_Filter(t *testing.T) {
assert.Nil(t, errs.Filter())
}

func TestErrors_Unwrap(t *testing.T) {
errs := Errors{
"B": errors.New("B1"),
"C": nil,
"A": errors.New("A1"),
}

unwrapped := errs.Unwrap()
assert.Contains(t, unwrapped, errs["B"])
assert.Contains(t, unwrapped, errs["A"])
assert.Len(t, unwrapped, 2)
}

func TestErrorObject_SetCode(t *testing.T) {
err := NewError("A", "msg").(ErrorObject)

Expand Down