Skip to content

Commit 86d97ef

Browse files
committed
format.Object now always includes err.Error() when passed an error
1 parent cc16689 commit 86d97ef

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

format/format.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var CharactersAroundMismatchToInclude uint = 5
5252
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
5353
var timeType = reflect.TypeOf(time.Time{})
5454

55-
//The default indentation string emitted by the format package
55+
// The default indentation string emitted by the format package
5656
var Indent = " "
5757

5858
var longFormThreshold = 20
@@ -258,7 +258,11 @@ Set PrintContextObjects to true to print the content of objects implementing con
258258
func Object(object interface{}, indentation uint) string {
259259
indent := strings.Repeat(Indent, int(indentation))
260260
value := reflect.ValueOf(object)
261-
return fmt.Sprintf("%s<%s>: %s", indent, formatType(value), formatValue(value, indentation))
261+
commonRepresentation := ""
262+
if err, ok := object.(error); ok {
263+
commonRepresentation += "\n" + IndentString(err.Error(), indentation)
264+
}
265+
return fmt.Sprintf("%s<%s>: %s%s", indent, formatType(value), formatValue(value, indentation), commonRepresentation)
262266
}
263267

264268
/*

format/format_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,20 @@ var _ = Describe("Format", func() {
612612
Expect(Object(t, 1)).Should(match("time.Time", `2016-10-31T09:57:23.000012345Z`))
613613
})
614614
})
615+
616+
Describe("formatting errors", func() {
617+
It("should include the error() representation", func() {
618+
err := fmt.Errorf("whoops: %w", fmt.Errorf("welp: %w", fmt.Errorf("ruh roh")))
619+
Expect(Object(err, 1)).Should(MatchRegexp(` \<\*fmt\.wrapError \| 0x[0-9a-f]*\>\: \{
620+
msg\: "whoops\: welp\: ruh roh",
621+
err\: \<\*fmt.wrapError \| 0x[0-9a-f]*\>\{
622+
msg\: "welp\: ruh roh",
623+
err\: \<\*errors.errorString \| 0x[0-9a-f]*\>\{s\: "ruh roh"\},
624+
\},
625+
\}
626+
whoops\: welp\: ruh roh`))
627+
})
628+
})
615629
})
616630

617631
Describe("Handling unexported fields in structs", func() {

matchers/have_occurred_matcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ func (matcher *HaveOccurredMatcher) FailureMessage(actual interface{}) (message
3131
}
3232

3333
func (matcher *HaveOccurredMatcher) NegatedFailureMessage(actual interface{}) (message string) {
34-
return fmt.Sprintf("Unexpected error:\n%s\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1), "occurred")
34+
return fmt.Sprintf("Unexpected error:\n%s\n%s", format.Object(actual, 1), "occurred")
3535
}

matchers/match_error_matcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ var _ = Describe("MatchErrorMatcher", func() {
135135
failuresMessages := InterceptGomegaFailures(func() {
136136
Expect(errors.New("foo")).To(MatchError("bar"))
137137
})
138-
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\nto match error\n <string>: bar"))
138+
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\n foo\nto match error\n <string>: bar"))
139139
})
140140

141141
It("shows negated failure message", func() {
142142
failuresMessages := InterceptGomegaFailures(func() {
143143
Expect(errors.New("foo")).ToNot(MatchError("foo"))
144144
})
145-
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\nnot to match error\n <string>: foo"))
145+
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\n foo\nnot to match error\n <string>: foo"))
146146
})
147147

148148
})

matchers/succeed_matcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (matcher *SucceedMatcher) FailureMessage(actual interface{}) (message strin
3434
if errors.As(actual.(error), &fgErr) {
3535
return fgErr.FormattedGomegaError()
3636
}
37-
return fmt.Sprintf("Expected success, but got an error:\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1))
37+
return fmt.Sprintf("Expected success, but got an error:\n%s", format.Object(actual, 1))
3838
}
3939

4040
func (matcher *SucceedMatcher) NegatedFailureMessage(actual interface{}) (message string) {

0 commit comments

Comments
 (0)