From aa5e552282c80bdb7bf51b8f10cea5428cd66588 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 18 May 2015 11:33:11 -0700 Subject: [PATCH] Fixed #65 Signed-off-by: Vishal Rana --- README.md | 8 +------- echo.go | 29 +++++++++++++++++------------ examples/crud/server.go | 2 +- examples/hello/server.go | 8 +------- examples/middleware/server.go | 6 +++--- examples/web/server.go | 2 +- router.go | 5 ++--- website/docs/index.md | 1 + 8 files changed, 27 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 0ff0b62b..2999b145 100644 --- a/README.md +++ b/README.md @@ -90,15 +90,9 @@ func main() { // Echo instance e := echo.New() - //------------ // Middleware - //------------ - - // Recover - e.Use(mw.Recover()) - - // Logger e.Use(mw.Logger()) + e.Use(mw.Recover()) // Routes e.Get("/", hello) diff --git a/echo.go b/echo.go index 7fa9417e..734400db 100644 --- a/echo.go +++ b/echo.go @@ -22,6 +22,7 @@ type ( prefix string middleware []MiddlewareFunc maxParam byte + notFoundHandler HandlerFunc httpErrorHandler HTTPErrorHandler binder BindFunc renderer Renderer @@ -134,13 +135,16 @@ func New() (e *Echo) { //---------- e.MaxParam(5) + e.notFoundHandler = func(c *Context) *HTTPError { + return &HTTPError{Code: http.StatusNotFound} + } e.HTTPErrorHandler(func(he *HTTPError, c *Context) { if he.Code == 0 { he.Code = http.StatusInternalServerError } if he.Message == "" { he.Message = http.StatusText(he.Code) - if e.debug { + if e.debug && he.Error != nil { he.Message = he.Error.Error() } } @@ -321,18 +325,19 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { } c.reset(w, r, e) if h == nil { - c.Error(&HTTPError{Code: http.StatusNotFound}) - } else { - // Chain middleware with handler in the end - for i := len(e.middleware) - 1; i >= 0; i-- { - h = e.middleware[i](h) - } - - // Execute chain - if he := h(c); he != nil { - e.httpErrorHandler(he, c) - } + h = e.notFoundHandler } + + // Chain middleware with handler in the end + for i := len(e.middleware) - 1; i >= 0; i-- { + h = e.middleware[i](h) + } + + // Execute chain + if he := h(c); he != nil { + e.httpErrorHandler(he, c) + } + e.pool.Put(c) } diff --git a/examples/crud/server.go b/examples/crud/server.go index 79d6f57b..00431447 100644 --- a/examples/crud/server.go +++ b/examples/crud/server.go @@ -61,8 +61,8 @@ func main() { e := echo.New() // Middleware - e.Use(mw.Recover()) e.Use(mw.Logger()) + e.Use(mw.Recover()) // Routes e.Post("/users", createUser) diff --git a/examples/hello/server.go b/examples/hello/server.go index 6a6a23cd..607cda86 100644 --- a/examples/hello/server.go +++ b/examples/hello/server.go @@ -16,15 +16,9 @@ func main() { // Echo instance e := echo.New() - //------------ // Middleware - //------------ - - // Recover - e.Use(mw.Recover()) - - // Logger e.Use(mw.Logger()) + e.Use(mw.Recover()) // Routes e.Get("/", hello) diff --git a/examples/middleware/server.go b/examples/middleware/server.go index 92e22b05..65201291 100644 --- a/examples/middleware/server.go +++ b/examples/middleware/server.go @@ -23,12 +23,12 @@ func main() { // Middleware //------------ - // Recover - e.Use(mw.Recover()) - // Logger e.Use(mw.Logger()) + // Recover + e.Use(mw.Recover()) + // Basic auth e.Use(mw.BasicAuth(func(u, p string) bool { if u == "joe" && p == "secret" { diff --git a/examples/web/server.go b/examples/web/server.go index 5f40dcae..81868962 100644 --- a/examples/web/server.go +++ b/examples/web/server.go @@ -65,8 +65,8 @@ func main() { e := echo.New() // Middleware - e.Use(mw.Recover()) e.Use(mw.Logger()) + e.Use(mw.Recover()) //------------------------ // Third-party middleware diff --git a/router.go b/router.go index 15956464..3cb4fff8 100644 --- a/router.go +++ b/router.go @@ -310,9 +310,8 @@ func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) { h, _ := r.Find(req.Method, req.URL.Path, c) c.reset(w, req, r.echo) if h == nil { - c.Error(&HTTPError{Code: http.StatusNotFound}) - } else { - h(c) + h = r.echo.notFoundHandler } + h(c) r.echo.pool.Put(c) } diff --git a/website/docs/index.md b/website/docs/index.md index fb69d1b5..883c57a1 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -67,6 +67,7 @@ func main() { // Middleware e.Use(mw.Logger()) + e.Use(mw.Recover()) // Routes e.Get("/", hello)