Skip to content

Commit

Permalink
Clear echo.Response if there is error while rendering.
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jul 8, 2015
1 parent 35d018c commit ba42c8f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
31 changes: 21 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{} {
Expand Down
5 changes: 5 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ba42c8f

Please sign in to comment.