Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make a pass at the changes for implementing issue-781 integrating pkg/errors #792

Merged
merged 3 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions middleware/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/goadesign/goa"
"github.com/pkg/errors"
"golang.org/x/net/context"
)

Expand All @@ -13,17 +14,18 @@ import (
// understands instances of goa.ServiceError and returns the status and response body embodied in
// them, it turns other Go error types into a 500 internal error response.
// If verbose is false the details of internal errors is not included in HTTP responses.
// If you use github.com/pkg/errors then wrapping the error will allow a trace to be printed to the logs
func ErrorHandler(service *goa.Service, verbose bool) goa.Middleware {
return func(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
e := h(ctx, rw, req)
if e == nil {
return nil
}

cause := errors.Cause(e)
status := http.StatusInternalServerError
var respBody interface{}
if err, ok := e.(goa.ServiceError); ok {
if err, ok := cause.(goa.ServiceError); ok {
status = err.ResponseStatus()
respBody = err
goa.ContextResponse(ctx).ErrorCode = err.Token()
Expand All @@ -38,7 +40,7 @@ func ErrorHandler(service *goa.Service, verbose bool) goa.Middleware {
reqID = shortID()
ctx = context.WithValue(ctx, reqIDKey, reqID)
}
goa.LogError(ctx, "uncaught error", "id", reqID, "msg", respBody)
goa.LogError(ctx, fmt.Sprintf("uncaught error : %+v", e), "id", reqID, "msg", respBody)

This comment was marked as off-topic.

This comment was marked as off-topic.

if !verbose {
rw.Header().Set("Content-Type", goa.ErrorMediaIdentifier)
msg := fmt.Sprintf("%s [%s]", http.StatusText(http.StatusInternalServerError), reqID)
Expand Down
34 changes: 34 additions & 0 deletions middleware/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"strings"

pErrors "github.com/pkg/errors"

"golang.org/x/net/context"

"github.com/goadesign/goa"
Expand Down Expand Up @@ -144,4 +146,36 @@ var _ = Describe("ErrorHandler", func() {
Ω(decoded.Error()).Should(Equal(gerr.Error()))
})
})

Context("with a handler returning a pkg errors wrapped error", func() {
var wrappedError error
var logger *testLogger
verbose = true
BeforeEach(func() {
logger = new(testLogger)
service = newService(logger)
wrappedError = pErrors.Wrap(goa.ErrInternal("something crazy happened"), "an error")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
return wrappedError
}
})

It("maps pkg errors to HTTP responses", func() {
var decoded errorResponse
cause := pErrors.Cause(wrappedError)
Ω(rw.Status).Should(Equal(cause.(goa.ServiceError).ResponseStatus()))
Ω(rw.ParentHeader["Content-Type"]).Should(Equal([]string{goa.ErrorMediaIdentifier}))
err := service.Decoder.Decode(&decoded, bytes.NewBuffer(rw.Body), "application/json")
Ω(err).ShouldNot(HaveOccurred())
Ω(decoded.Error()).Should(Equal(cause.Error()))
})
It("logs pkg errors stacktaces", func() {
var decoded errorResponse
err := service.Decoder.Decode(&decoded, bytes.NewBuffer(rw.Body), "application/json")
Ω(err).ShouldNot(HaveOccurred())
Ω(logger.ErrorEntries).Should(HaveLen(1))
entry := logger.ErrorEntries[0].Msg
Ω(entry).Should(ContainSubstring("error_handler_test.go"))
})
})
})