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:
parent
5ac2b8abfa
commit
1ae7ef40e0
@ -26,6 +26,7 @@ A fast and unfancy micro web framework for Golang.
|
||||
- HTML via templates
|
||||
- String
|
||||
- JSON
|
||||
- JSONP
|
||||
- XML
|
||||
- NoContent
|
||||
- Redirect
|
||||
|
27
context.go
27
context.go
@ -115,10 +115,7 @@ 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
|
||||
@ -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) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
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
|
||||
@ -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
|
||||
@ -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.
|
||||
|
6
echo.go
6
echo.go
@ -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) {
|
||||
|
@ -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
|
||||
//}
|
||||
|
@ -28,6 +28,7 @@ A fast and unfancy micro web framework for Golang.
|
||||
- HTML via templates
|
||||
- String
|
||||
- JSON
|
||||
- JSONP
|
||||
- XML
|
||||
- NoContent
|
||||
- Redirect
|
||||
|
Loading…
Reference in New Issue
Block a user