Skip to content

Commit

Permalink
format.Object now always includes err.Error() when passed an error
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Mar 10, 2023
1 parent cc16689 commit 86d97ef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
8 changes: 6 additions & 2 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var CharactersAroundMismatchToInclude uint = 5
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
var timeType = reflect.TypeOf(time.Time{})

//The default indentation string emitted by the format package
// The default indentation string emitted by the format package
var Indent = " "

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

/*
Expand Down
14 changes: 14 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,20 @@ var _ = Describe("Format", func() {
Expect(Object(t, 1)).Should(match("time.Time", `2016-10-31T09:57:23.000012345Z`))
})
})

Describe("formatting errors", func() {
It("should include the error() representation", func() {
err := fmt.Errorf("whoops: %w", fmt.Errorf("welp: %w", fmt.Errorf("ruh roh")))
Expect(Object(err, 1)).Should(MatchRegexp(` \<\*fmt\.wrapError \| 0x[0-9a-f]*\>\: \{
msg\: "whoops\: welp\: ruh roh",
err\: \<\*fmt.wrapError \| 0x[0-9a-f]*\>\{
msg\: "welp\: ruh roh",
err\: \<\*errors.errorString \| 0x[0-9a-f]*\>\{s\: "ruh roh"\},
\},
\}
whoops\: welp\: ruh roh`))
})
})
})

Describe("Handling unexported fields in structs", func() {
Expand Down
2 changes: 1 addition & 1 deletion matchers/have_occurred_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func (matcher *HaveOccurredMatcher) FailureMessage(actual interface{}) (message
}

func (matcher *HaveOccurredMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Unexpected error:\n%s\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1), "occurred")
return fmt.Sprintf("Unexpected error:\n%s\n%s", format.Object(actual, 1), "occurred")
}
4 changes: 2 additions & 2 deletions matchers/match_error_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ var _ = Describe("MatchErrorMatcher", func() {
failuresMessages := InterceptGomegaFailures(func() {
Expect(errors.New("foo")).To(MatchError("bar"))
})
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\nto match error\n <string>: bar"))
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\n foo\nto match error\n <string>: bar"))
})

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

})
Expand Down
2 changes: 1 addition & 1 deletion matchers/succeed_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (matcher *SucceedMatcher) FailureMessage(actual interface{}) (message strin
if errors.As(actual.(error), &fgErr) {
return fgErr.FormattedGomegaError()
}
return fmt.Sprintf("Expected success, but got an error:\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1))
return fmt.Sprintf("Expected success, but got an error:\n%s", format.Object(actual, 1))
}

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

0 comments on commit 86d97ef

Please sign in to comment.