From ba42c8f32cd599a0f3a0d3dd575993d2c153b3c0 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 7 Jul 2015 17:34:59 -0700 Subject: [PATCH] Clear `echo.Response` if there is error while rendering. Signed-off-by: Vishal Rana --- context.go | 31 +++++++++++++++++++++---------- echo.go | 5 +++-- response.go | 5 +++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/context.go b/context.go index 67bdf09ca..7031c4a19 100644 --- a/context.go +++ b/context.go @@ -104,38 +104,49 @@ func (c *Context) Bind(i interface{}) error { // Render invokes the registered HTML template renderer and sends a text/html // response with status code. -func (c *Context) Render(code int, name string, data interface{}) error { +func (c *Context) Render(code int, name string, data interface{}) (err error) { if c.echo.renderer == nil { return RendererNotRegistered } c.response.Header().Set(ContentType, TextHTML) c.response.WriteHeader(code) - return c.echo.renderer.Render(c.response, name, data) + if err = c.echo.renderer.Render(c.response, name, data); err != nil { + println(err.Error()) + c.response.clear() + } + return } // JSON sends an application/json response with status code. -func (c *Context) JSON(code int, i interface{}) error { +func (c *Context) JSON(code int, i interface{}) (err error) { c.response.Header().Set(ContentType, ApplicationJSON) c.response.WriteHeader(code) - return json.NewEncoder(c.response).Encode(i) + if err = json.NewEncoder(c.response).Encode(i); err != nil { + c.response.clear() + } + return } // String formats according to a format specifier and sends text/plain response // with status code. -func (c *Context) String(code int, format string, a ...interface{}) error { +func (c *Context) String(code int, format string, a ...interface{}) (err error) { c.response.Header().Set(ContentType, TextPlain) c.response.WriteHeader(code) - _, err := fmt.Fprintf(c.response, format, a...) - return err + if _, err = fmt.Fprintf(c.response, format, a...); err != nil { + c.response.clear() + } + return } // HTML formats according to a format specifier and sends text/html response with // status code. -func (c *Context) HTML(code int, format string, a ...interface{}) error { +func (c *Context) HTML(code int, format string, a ...interface{}) (err error) { c.response.Header().Set(ContentType, TextHTML) c.response.WriteHeader(code) - _, err := fmt.Fprintf(c.response, format, a...) - return err + if _, err = fmt.Fprintf(c.response, format, a...); err != nil { + c.response.clear() + } + return } // NoContent sends a response with no body and a status code. diff --git a/echo.go b/echo.go index 1c5766149..1f3080be8 100644 --- a/echo.go +++ b/echo.go @@ -103,11 +103,12 @@ const ( Accept = "Accept" AcceptEncoding = "Accept-Encoding" + Authorization = "Authorization" ContentDisposition = "Content-Disposition" ContentEncoding = "Content-Encoding" ContentLength = "Content-Length" ContentType = "Content-Type" - Authorization = "Authorization" + Location = "Location" Upgrade = "Upgrade" Vary = "Vary" @@ -141,7 +142,7 @@ var ( RendererNotRegistered = errors.New("echo ⇒ renderer not registered") ) -// New creates an Echo instance. +// New creates an instance of Echo. func New() (e *Echo) { e = &Echo{maxParam: new(int)} e.pool.New = func() interface{} { diff --git a/response.go b/response.go index 84de7c9c7..ae26a32fe 100644 --- a/response.go +++ b/response.go @@ -80,3 +80,8 @@ func (r *Response) reset(w http.ResponseWriter) { r.status = http.StatusOK r.committed = false } + +func (r *Response) clear() { + r.Header().Del(ContentType) + r.committed = false +}