Skip to content

Commit 95a6a89

Browse files
bits01Raphaël Simon
authored and
Raphaël Simon
committed
Copy original error ID in the error handler if not verbose (goadesign#641)
1 parent a843c43 commit 95a6a89

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

middleware/error_handler.go

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func ErrorHandler(service *goa.Service, verbose bool) goa.Middleware {
4343
rw.Header().Set("Content-Type", goa.ErrorMediaIdentifier)
4444
msg := fmt.Sprintf("%s [%s]", http.StatusText(http.StatusInternalServerError), reqID)
4545
respBody = goa.ErrInternal(msg)
46+
// Preserve the ID of the original error as that's what gets logged, the client
47+
// received error ID must match the original
48+
if origErrID := goa.ContextResponse(ctx).ErrorCode; origErrID != "" {
49+
respBody.(*goa.ErrorResponse).ID = origErrID
50+
}
4651
}
4752
}
4853
return service.Send(ctx, status, respBody)

middleware/error_handler_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ var _ = Describe("ErrorHandler", func() {
9898
msg = `\[.*\]` + msg[endIDidx+1:]
9999
Ω(fmt.Sprintf("%v", decoded.Error())).Should(MatchRegexp(msg))
100100
})
101+
102+
Context("and goa 500 error", func() {
103+
var origID string
104+
105+
BeforeEach(func() {
106+
verbose = false
107+
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
108+
e := goa.ErrInternal("goa-500-boom")
109+
origID = e.(goa.ServiceError).Token()
110+
return e
111+
}
112+
})
113+
114+
It("preserves the error ID from the original error", func() {
115+
var decoded errorResponse
116+
Ω(origID).ShouldNot(Equal(""))
117+
Ω(rw.Status).Should(Equal(500))
118+
Ω(rw.ParentHeader["Content-Type"]).Should(Equal([]string{goa.ErrorMediaIdentifier}))
119+
err := service.Decoder.Decode(&decoded, bytes.NewBuffer(rw.Body), "application/json")
120+
Ω(err).ShouldNot(HaveOccurred())
121+
Ω(decoded.ID).Should(Equal(origID))
122+
})
123+
})
101124
})
102125
})
103126

0 commit comments

Comments
 (0)