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

Don't send http error if response already committed.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-07-24 18:54:42 -07:00
parent 5ac2b8abfa
commit 1ae7ef40e0
5 changed files with 17 additions and 26 deletions

View File

@ -26,6 +26,7 @@ A fast and unfancy micro web framework for Golang.
- HTML via templates - HTML via templates
- String - String
- JSON - JSON
- JSONP
- XML - XML
- NoContent - NoContent
- Redirect - Redirect

View File

@ -115,10 +115,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
} }
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
if err = c.echo.renderer.Render(c.response, name, data); err != nil { return c.echo.renderer.Render(c.response, name, data)
c.response.clear()
}
return
} }
// HTML formats according to a format specifier and sends HTML response with // HTML formats according to a format specifier and sends HTML response with
@ -126,9 +123,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) { func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
if _, err = fmt.Fprintf(c.response, format, a...); err != nil { _, err = fmt.Fprintf(c.response, format, a...)
c.response.clear()
}
return return
} }
@ -137,9 +132,7 @@ func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
func (c *Context) String(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.Header().Set(ContentType, TextPlain)
c.response.WriteHeader(code) c.response.WriteHeader(code)
if _, err = fmt.Fprintf(c.response, format, a...); err != nil { _, err = fmt.Fprintf(c.response, format, a...)
c.response.clear()
}
return return
} }
@ -147,10 +140,7 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error)
func (c *Context) JSON(code int, i interface{}) (err error) { func (c *Context) JSON(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8) c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
if err = json.NewEncoder(c.response).Encode(i); err != nil { return json.NewEncoder(c.response).Encode(i)
c.response.clear()
}
return
} }
// JSONP sends a JSONP response with status code. It uses `callback` to construct // JSONP sends a JSONP response with status code. It uses `callback` to construct
@ -159,9 +149,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8) c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
c.response.Write([]byte(callback + "(")) c.response.Write([]byte(callback + "("))
if err = json.NewEncoder(c.response).Encode(i); err != nil { if err = json.NewEncoder(c.response).Encode(i); err == nil {
c.response.clear()
} else {
c.response.Write([]byte(");")) c.response.Write([]byte(");"))
} }
return return
@ -172,10 +160,7 @@ func (c *Context) XML(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8) c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header)) c.response.Write([]byte(xml.Header))
if err = xml.NewEncoder(c.response).Encode(i); err != nil { return xml.NewEncoder(c.response).Encode(i)
c.response.clear()
}
return
} }
// NoContent sends a response with no body and a status code. // NoContent sends a response with no body and a status code.

View File

@ -195,7 +195,11 @@ func New() (e *Echo) {
if e.debug { if e.debug {
msg = err.Error() 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.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(func(r *http.Request, v interface{}) (err error) { e.SetBinder(func(r *http.Request, v interface{}) (err error) {

View File

@ -81,7 +81,7 @@ func (r *Response) reset(w http.ResponseWriter) {
r.committed = false r.committed = false
} }
func (r *Response) clear() { //func (r *Response) clear() {
r.Header().Del(ContentType) // r.Header().Del(ContentType)
r.committed = false // r.committed = false
} //}

View File

@ -28,6 +28,7 @@ A fast and unfancy micro web framework for Golang.
- HTML via templates - HTML via templates
- String - String
- JSON - JSON
- JSONP
- XML - XML
- NoContent - NoContent
- Redirect - Redirect