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{}
)
// NewContext creates a Context object.
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
return &Context{
request: req,

40
echo.go
View File

@ -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

View File

@ -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 {

View File

@ -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")