Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
[ALT] Overriding app.ErrorMiddleware has no effect fixes #1466 (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Feb 3, 2019
1 parent 04f308d commit a6e2fa5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
20 changes: 8 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import (
type App struct {
Options
// Middleware returns the current MiddlewareStack for the App/Group.
Middleware *MiddlewareStack `json:"-"`
ErrorHandlers ErrorHandlers `json:"-"`
ErrorMiddleware MiddlewareFunc `json:"-"`
router *mux.Router
moot *sync.RWMutex
routes RouteList
root *App
children []*App
filepaths []string
Middleware *MiddlewareStack `json:"-"`
ErrorHandlers ErrorHandlers `json:"-"`
router *mux.Router
moot *sync.RWMutex
routes RouteList
root *App
children []*App
filepaths []string
}

// Muxer returns the underlying mux router to allow
Expand Down Expand Up @@ -52,9 +51,6 @@ func New(opts Options) *App {
}

dem := a.defaultErrorMiddleware
if a.ErrorMiddleware != nil {
dem = a.ErrorMiddleware
}
a.Middleware = newMiddlewareStack(dem)

notFoundHandler := func(errorf string, code int) http.HandlerFunc {
Expand Down
11 changes: 11 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ func (e ErrorHandlers) Get(status int) ErrorHandler {
if eh, ok := e[status]; ok {
return eh
}
if eh, ok := e[0]; ok {
return eh
}
return defaultErrorHandler
}

// Default sets an error handler should a status
// code not already be mapped. This will replace
// the original default error handler.
// This is a *catch-all* handler.
func (e ErrorHandlers) Default(eh ErrorHandler) {
e[0] = eh
}

// PanicHandler recovers from panics gracefully and calls
// the error handling code for a 500 error.
func (a *App) PanicHandler(next Handler) Handler {
Expand Down
19 changes: 19 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,22 @@ func Test_defaultErrorMiddleware(t *testing.T) {
r.True(ok)
r.Equal("t", x)
}

func Test_SetErrorMiddleware(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.ErrorHandlers.Default(func(code int, err error, c Context) error {
res := c.Response()
res.WriteHeader(418)
res.Write([]byte("i'm a teapot"))
return nil
})
app.GET("/", func(c Context) error {
return c.Error(422, errors.New("boom"))
})

w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(418, res.Code)
r.Equal("i'm a teapot", res.Body.String())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ require (
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
golang.org/x/tools v0.0.0-20190201185910-0e05534988fe
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ golang.org/x/tools v0.0.0-20190118193359-16909d206f00/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190122202912-9c309ee22fab/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190124004107-78ee07aa9465/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190131142011-8dbcc66f33bb/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190201185910-0e05534988fe h1:vnIBlz9Fwms9Qgql2K0Ar+XatQxNe3xuRwzm4XtKZJ8=
golang.org/x/tools v0.0.0-20190201185910-0e05534988fe/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c h1:6Axm8Kqba7gHaI7My7snFynbKaVEYko0z35GPOJygUA=
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
Expand Down
6 changes: 5 additions & 1 deletion route_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ func (ri RouteInfo) ServeHTTP(res http.ResponseWriter, req *http.Request) {
err := a.Middleware.handler(ri)(c)

if err != nil {
status := 500
if he, ok := err.(HTTPError); ok {
status = he.Status
}
events.EmitError(EvtRouteErr, err, payload)
// things have really hit the fan if we're here!!
a.Logger.Error(err)
c.Response().WriteHeader(500)
c.Response().WriteHeader(status)
c.Response().Write([]byte(err.Error()))
}
events.EmitPayload(EvtRouteFinished, payload)
Expand Down

0 comments on commit a6e2fa5

Please sign in to comment.