Skip to content
Permalink
Browse files

[ALT] Overriding app.ErrorMiddleware has no effect fixes #1466 (#1564)

  • Loading branch information...
markbates committed Feb 3, 2019
1 parent 04f308d commit a6e2fa562e74c6c8bbb58bcdb37a2216bcf8a585
Showing with 46 additions and 16 deletions.
  1. +8 −12 app.go
  2. +11 −0 errors.go
  3. +19 −0 errors_test.go
  4. +1 −1 go.mod
  5. +2 −2 go.sum
  6. +5 −1 route_info.go
20 app.go
@@ -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
@@ -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 {
@@ -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 {
@@ -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 go.mod
@@ -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 go.sum
@@ -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=
@@ -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)

0 comments on commit a6e2fa5

Please sign in to comment.
You can’t perform that action at this time.