1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

Clear echo.Response if there is error while rendering.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-07-07 17:34:59 -07:00
parent 35d018c671
commit ba42c8f32c
3 changed files with 29 additions and 12 deletions

View File

@ -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.

View File

@ -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{} {

View File

@ -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
}