1
0
mirror of https://github.com/labstack/echo.git synced 2025-02-03 13:11:39 +02:00

Cleaned up http error handler #325

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-01-22 13:37:18 -08:00
parent c67300bb30
commit 106272e201
2 changed files with 15 additions and 32 deletions

14
echo.go
View File

@ -12,7 +12,6 @@ import (
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"time"
"encoding/xml" "encoding/xml"
@ -27,8 +26,6 @@ type (
middleware []MiddlewareFunc middleware []MiddlewareFunc
http2 bool http2 bool
maxParam *int maxParam *int
notFoundHandler HandlerFunc
defaultHTTPErrorHandler HTTPErrorHandler
httpErrorHandler HTTPErrorHandler httpErrorHandler HTTPErrorHandler
binder Binder binder Binder
renderer Renderer renderer Renderer
@ -180,8 +177,6 @@ var (
methodNotAllowedHandler = func(c *Context) error { methodNotAllowedHandler = func(c *Context) error {
return NewHTTPError(http.StatusMethodNotAllowed) return NewHTTPError(http.StatusMethodNotAllowed)
} }
unixEpochTime = time.Unix(0, 0)
) )
// New creates an instance of Echo. // New creates an instance of Echo.
@ -197,7 +192,7 @@ func New() (e *Echo) {
//---------- //----------
e.HTTP2(true) e.HTTP2(true)
e.defaultHTTPErrorHandler = func(err error, c *Context) { defaultHTTPErrorHandler := func(err error, c *Context) {
code := http.StatusInternalServerError code := http.StatusInternalServerError
msg := http.StatusText(code) msg := http.StatusText(code)
if he, ok := err.(*HTTPError); ok { if he, ok := err.(*HTTPError); ok {
@ -212,7 +207,7 @@ func New() (e *Echo) {
} }
e.logger.Error(err) e.logger.Error(err)
} }
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler) e.SetHTTPErrorHandler(defaultHTTPErrorHandler)
e.SetBinder(&binder{}) e.SetBinder(&binder{})
// Logger // Logger
@ -252,11 +247,6 @@ func (e *Echo) HTTP2(on bool) {
e.http2 = on e.http2 = on
} }
// 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. // SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) { func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) {
e.httpErrorHandler = h e.httpErrorHandler = h

View File

@ -25,9 +25,6 @@ type (
func TestEcho(t *testing.T) { func TestEcho(t *testing.T) {
e := New() e := New()
req, _ := http.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := NewContext(req, NewResponse(rec, e), e)
// Router // Router
assert.NotNil(t, e.Router()) assert.NotNil(t, e.Router())
@ -35,10 +32,6 @@ func TestEcho(t *testing.T) {
// Debug // Debug
e.SetDebug(true) e.SetDebug(true)
assert.True(t, e.debug) assert.True(t, e.debug)
// DefaultHTTPErrorHandler
e.DefaultHTTPErrorHandler(errors.New("error"), c)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
} }
func TestEchoIndex(t *testing.T) { func TestEchoIndex(t *testing.T) {