Skip to content

Commit 4b678c9

Browse files
author
Ronen Hilewicz
committed
AsertoError.Fields() includes fields from inner errors
1 parent 0fe7f5a commit 4b678c9

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func WrapContext(err error, ctx context.Context, message string) *ContextError {
2424
return WithContext(errors.Wrap(err, message), ctx)
2525
}
2626

27-
func WrapfContext(err error, ctx context.Context, format string, args ...interface{}) *ContextError {
27+
func WrapfContext(err error, ctx context.Context, format string, args ...any) *ContextError {
2828
return WithContext(errors.Wrapf(err, format, args...), ctx)
2929
}
3030

errors.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"maps"
78
"net/http"
89
"strconv"
910
"strings"
@@ -109,8 +110,15 @@ func (e *AsertoError) Error() string {
109110
return fmt.Sprintf("%s %s: %s", e.Code, e.Message, innerMessage)
110111
}
111112

112-
func (e *AsertoError) Fields() map[string]interface{} {
113-
result := make(map[string]interface{}, len(e.data))
113+
func (e *AsertoError) Fields() map[string]any {
114+
result := make(map[string]any, len(e.data))
115+
116+
for _, err := range e.errs {
117+
var aerr *AsertoError
118+
if ok := errors.As(err, &aerr); ok {
119+
maps.Copy(result, aerr.Fields())
120+
}
121+
}
114122

115123
for k, v := range e.data {
116124
result[k] = v
@@ -145,7 +153,7 @@ func (e *AsertoError) Msg(message string) *AsertoError {
145153
return c
146154
}
147155

148-
func (e *AsertoError) Msgf(message string, args ...interface{}) *AsertoError {
156+
func (e *AsertoError) Msgf(message string, args ...any) *AsertoError {
149157
c := e.Copy()
150158

151159
message = fmt.Sprintf(message, args...)
@@ -221,7 +229,7 @@ func (e *AsertoError) FromReader(key string, value io.Reader) *AsertoError {
221229
return c
222230
}
223231

224-
func (e *AsertoError) Interface(key string, value interface{}) *AsertoError {
232+
func (e *AsertoError) Interface(key string, value any) *AsertoError {
225233
c := e.Copy()
226234
c.data[key] = fmt.Sprintf("%+v", value)
227235

errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestWithEmptyMsg(t *testing.T) {
4949
err := ErrNotFound.Msg("")
5050

5151
fields := err.Fields()
52-
assert.Nil(fields[cerr.MessageKey])
52+
assert.Empty(fields[cerr.MessageKey])
5353

5454
err = ErrNotFound.Msg("bla")
5555

0 commit comments

Comments
 (0)