1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-24 03:16:14 +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

40
echo.go
View File

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

View File

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