From 76e9b44190af2e2efebc018639aecfd762e8ed97 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 28 May 2015 16:50:49 -0700 Subject: [PATCH] Exposed DefaultHTTPErrorHandler function Signed-off-by: Vishal Rana --- context.go | 1 + echo.go | 40 ++++++++++++++++++++++++---------------- middleware/slash.go | 10 +++++----- response_test.go | 4 ++-- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/context.go b/context.go index 3e36abcc..357c6e6b 100644 --- a/context.go +++ b/context.go @@ -22,6 +22,7 @@ type ( store map[string]interface{} ) +// NewContext creates a Context object. func NewContext(req *http.Request, res *Response, e *Echo) *Context { return &Context{ request: req, diff --git a/echo.go b/echo.go index b7aaa589..195d7c28 100644 --- a/echo.go +++ b/echo.go @@ -22,18 +22,19 @@ import ( type ( Echo struct { - router *Router - prefix string - middleware []MiddlewareFunc - http2 bool - maxParam byte - notFoundHandler HandlerFunc - httpErrorHandler HTTPErrorHandler - binder BindFunc - renderer Renderer - uris map[Handler]string - pool sync.Pool - debug bool + router *Router + prefix string + middleware []MiddlewareFunc + http2 bool + maxParam byte + notFoundHandler HandlerFunc + defaultHTTPErrorHandler HTTPErrorHandler + httpErrorHandler HTTPErrorHandler + binder BindFunc + renderer Renderer + uris map[Handler]string + pool sync.Pool + debug bool } HTTPError struct { @@ -152,7 +153,7 @@ func New() (e *Echo) { e.notFoundHandler = func(c *Context) error { return NewHTTPError(http.StatusNotFound) } - e.SetHTTPErrorHandler(func(err error, c *Context) { + e.defaultHTTPErrorHandler = func(err error, c *Context) { code := http.StatusInternalServerError msg := http.StatusText(code) if he, ok := err.(*HTTPError); ok { @@ -163,7 +164,8 @@ func New() (e *Echo) { msg = err.Error() } http.Error(c.response, msg, code) - }) + } + e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler) e.SetBinder(func(r *http.Request, v interface{}) error { ct := r.Header.Get(ContentType) err := UnsupportedMediaType @@ -193,7 +195,12 @@ func (e *Echo) SetMaxParam(n uint8) { e.maxParam = n } -// SetHTTPErrorHandler registers an Echo.HTTPErrorHandler. +// DefaultHTTPErrorHandler invokes the default HTTP error handler. +func (e *Echo) DefaultHTTPErrorHandler(err error, c *Context) { + e.defaultHTTPErrorHandler(err, c) +} + +// SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler. func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) { e.httpErrorHandler = h } @@ -441,7 +448,8 @@ func (e *Echo) run(s *http.Server, files ...string) { func NewHTTPError(code int, msg ...string) *HTTPError { he := &HTTPError{code: code, message: http.StatusText(code)} - for _, m := range msg { + if len(msg) > 0 { + m := msg[0] he.message = m } return he diff --git a/middleware/slash.go b/middleware/slash.go index acd4bef5..a768a8ec 100644 --- a/middleware/slash.go +++ b/middleware/slash.go @@ -1,8 +1,9 @@ package middleware import ( - "github.com/labstack/echo" "net/http" + + "github.com/labstack/echo" ) type ( @@ -29,10 +30,9 @@ func StripTrailingSlash() echo.HandlerFunc { func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc { code := http.StatusMovedPermanently - for _, o := range opts { - if o.Code != 0 { - code = o.Code - } + if len(opts) > 0 { + o := opts[0] + code = o.Code } return func(c *echo.Context) error { diff --git a/response_test.go b/response_test.go index f7296c2a..db07652a 100644 --- a/response_test.go +++ b/response_test.go @@ -37,8 +37,8 @@ func TestResponse(t *testing.T) { if r.Size() != int64(len(s)) { t.Errorf("size should be %d", len(s)) } - - // reser + + // reset r.reset(httptest.NewRecorder()) if r.Size() != int64(0) { t.Error("size should be 0")