Skip to content

Commit ed79acf

Browse files
Improve API errors message
Include details and optional cause error in the message fixes #4256 Co-authored-by: Georgi Sabev <[email protected]>
1 parent a548cbf commit ed79acf

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

api/errors/errors.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ type apiError struct {
4747
}
4848

4949
func (e apiError) Error() string {
50-
if e.cause == nil {
51-
return "unknown"
50+
detail := e.Detail()
51+
if e.cause != nil {
52+
return fmt.Sprintf("%s: %s", detail, e.cause.Error())
5253
}
5354

54-
return e.cause.Error()
55+
if detail != "" {
56+
return detail
57+
}
58+
59+
return "unknown"
5560
}
5661

5762
func (e apiError) Unwrap() error {

api/errors/errors_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ var _ = Describe("LogAndReturn", func() {
288288
Expect(logEntry["level"]).To(Equal("info"))
289289
Expect(logEntry["msg"]).To(Equal("some message"))
290290
Expect(logEntry["some-key"]).To(Equal("some-value"))
291-
Expect(logEntry["reason"]).To(Equal("cause-err"))
291+
Expect(logEntry["reason"]).To(ContainSubstring("cause-err"))
292292
})
293293
})
294294

@@ -301,7 +301,7 @@ var _ = Describe("LogAndReturn", func() {
301301
Expect(logEntry["level"]).To(Equal("info"))
302302
Expect(logEntry["msg"]).To(Equal("some message"))
303303
Expect(logEntry["some-key"]).To(Equal("some-value"))
304-
Expect(logEntry["reason"]).To(Equal("wrapping: cause-err"))
304+
Expect(logEntry["reason"]).To(MatchRegexp("wrapping:.*cause-err"))
305305
})
306306
})
307307
})
@@ -355,14 +355,35 @@ var _ = Describe("invalid lists", func() {
355355
})
356356
})
357357

358-
type testApiError struct {
359-
apierrors.ApiError
360-
}
358+
var _ = Describe("Error", func() {
359+
var (
360+
err apierrors.ApiError
361+
errorString string
362+
)
361363

362-
func (e testApiError) Error() string {
363-
return ""
364-
}
364+
BeforeEach(func() {
365+
err = apierrors.NewUnknownError(errors.New("oops"))
366+
})
367+
368+
JustBeforeEach(func() {
369+
errorString = err.Error()
370+
})
365371

366-
func (e testApiError) Unwrap() error {
367-
return nil
372+
It("returns error details and cause", func() {
373+
Expect(errorString).To(Equal("An unknown error occurred.: oops"))
374+
})
375+
376+
When("the error has no cause", func() {
377+
BeforeEach(func() {
378+
err = apierrors.NewUnknownError(nil)
379+
})
380+
381+
It("returns details only", func() {
382+
Expect(errorString).To(Equal("An unknown error occurred."))
383+
})
384+
})
385+
})
386+
387+
type testApiError struct {
388+
apierrors.ApiError
368389
}

0 commit comments

Comments
 (0)