Skip to content

Commit

Permalink
Don't send http error if response already committed.
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jul 25, 2015
1 parent 5ac2b8a commit 1ae7ef4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ A fast and unfancy micro web framework for Golang.
- HTML via templates
- String
- JSON
- JSONP
- XML
- NoContent
- Redirect
Expand Down
27 changes: 6 additions & 21 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,15 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
}
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code)
if err = c.echo.renderer.Render(c.response, name, data); err != nil {
c.response.clear()
}
return
return c.echo.renderer.Render(c.response, name, data)
}

// HTML formats according to a format specifier and sends HTML response with
// status code.
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code)
if _, err = fmt.Fprintf(c.response, format, a...); err != nil {
c.response.clear()
}
_, err = fmt.Fprintf(c.response, format, a...)
return
}

Expand All @@ -137,20 +132,15 @@ func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
func (c *Context) String(code int, format string, a ...interface{}) (err error) {
c.response.Header().Set(ContentType, TextPlain)
c.response.WriteHeader(code)
if _, err = fmt.Fprintf(c.response, format, a...); err != nil {
c.response.clear()
}
_, err = fmt.Fprintf(c.response, format, a...)
return
}

// JSON sends a JSON response with status code.
func (c *Context) JSON(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
if err = json.NewEncoder(c.response).Encode(i); err != nil {
c.response.clear()
}
return
return json.NewEncoder(c.response).Encode(i)
}

// JSONP sends a JSONP response with status code. It uses `callback` to construct
Expand All @@ -159,9 +149,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(callback + "("))
if err = json.NewEncoder(c.response).Encode(i); err != nil {
c.response.clear()
} else {
if err = json.NewEncoder(c.response).Encode(i); err == nil {
c.response.Write([]byte(");"))
}
return
Expand All @@ -172,10 +160,7 @@ func (c *Context) XML(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header))
if err = xml.NewEncoder(c.response).Encode(i); err != nil {
c.response.clear()
}
return
return xml.NewEncoder(c.response).Encode(i)
}

// NoContent sends a response with no body and a status code.
Expand Down
6 changes: 5 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ func New() (e *Echo) {
if e.debug {
msg = err.Error()
}
http.Error(c.response, msg, code)
if !c.response.committed {
http.Error(c.response, msg, code)
}
// TODO:
// else just send the message? log?
}
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(func(r *http.Request, v interface{}) (err error) {
Expand Down
8 changes: 4 additions & 4 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r *Response) reset(w http.ResponseWriter) {
r.committed = false
}

func (r *Response) clear() {
r.Header().Del(ContentType)
r.committed = false
}
//func (r *Response) clear() {
// r.Header().Del(ContentType)
// r.committed = false
//}
1 change: 1 addition & 0 deletions website/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A fast and unfancy micro web framework for Golang.
- HTML via templates
- String
- JSON
- JSONP
- XML
- NoContent
- Redirect
Expand Down

0 comments on commit 1ae7ef4

Please sign in to comment.