1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Exposed DefaultHTTPErrorHandler function

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-28 16:50:49 -07:00
parent 7d518ce19d
commit 76e9b44190
4 changed files with 32 additions and 23 deletions

View File

@ -22,6 +22,7 @@ type (
store map[string]interface{} store map[string]interface{}
) )
// NewContext creates a Context object.
func NewContext(req *http.Request, res *Response, e *Echo) *Context { func NewContext(req *http.Request, res *Response, e *Echo) *Context {
return &Context{ return &Context{
request: req, request: req,

40
echo.go
View File

@ -22,18 +22,19 @@ import (
type ( type (
Echo struct { Echo struct {
router *Router router *Router
prefix string prefix string
middleware []MiddlewareFunc middleware []MiddlewareFunc
http2 bool http2 bool
maxParam byte maxParam byte
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler defaultHTTPErrorHandler HTTPErrorHandler
binder BindFunc httpErrorHandler HTTPErrorHandler
renderer Renderer binder BindFunc
uris map[Handler]string renderer Renderer
pool sync.Pool uris map[Handler]string
debug bool pool sync.Pool
debug bool
} }
HTTPError struct { HTTPError struct {
@ -152,7 +153,7 @@ func New() (e *Echo) {
e.notFoundHandler = func(c *Context) error { e.notFoundHandler = func(c *Context) error {
return NewHTTPError(http.StatusNotFound) return NewHTTPError(http.StatusNotFound)
} }
e.SetHTTPErrorHandler(func(err error, c *Context) { e.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 {
@ -163,7 +164,8 @@ func New() (e *Echo) {
msg = err.Error() msg = err.Error()
} }
http.Error(c.response, msg, code) http.Error(c.response, msg, code)
}) }
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(func(r *http.Request, v interface{}) error { e.SetBinder(func(r *http.Request, v interface{}) error {
ct := r.Header.Get(ContentType) ct := r.Header.Get(ContentType)
err := UnsupportedMediaType err := UnsupportedMediaType
@ -193,7 +195,12 @@ func (e *Echo) SetMaxParam(n uint8) {
e.maxParam = n 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) { func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) {
e.httpErrorHandler = h e.httpErrorHandler = h
} }
@ -441,7 +448,8 @@ func (e *Echo) run(s *http.Server, files ...string) {
func NewHTTPError(code int, msg ...string) *HTTPError { func NewHTTPError(code int, msg ...string) *HTTPError {
he := &HTTPError{code: code, message: http.StatusText(code)} he := &HTTPError{code: code, message: http.StatusText(code)}
for _, m := range msg { if len(msg) > 0 {
m := msg[0]
he.message = m he.message = m
} }
return he return he

View File

@ -1,8 +1,9 @@
package middleware package middleware
import ( import (
"github.com/labstack/echo"
"net/http" "net/http"
"github.com/labstack/echo"
) )
type ( type (
@ -29,10 +30,9 @@ func StripTrailingSlash() echo.HandlerFunc {
func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc { func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc {
code := http.StatusMovedPermanently code := http.StatusMovedPermanently
for _, o := range opts { if len(opts) > 0 {
if o.Code != 0 { o := opts[0]
code = o.Code code = o.Code
}
} }
return func(c *echo.Context) error { return func(c *echo.Context) error {

View File

@ -37,8 +37,8 @@ func TestResponse(t *testing.T) {
if r.Size() != int64(len(s)) { if r.Size() != int64(len(s)) {
t.Errorf("size should be %d", len(s)) t.Errorf("size should be %d", len(s))
} }
// reser // reset
r.reset(httptest.NewRecorder()) r.reset(httptest.NewRecorder())
if r.Size() != int64(0) { if r.Size() != int64(0) {
t.Error("size should be 0") t.Error("size should be 0")