Write header immediately in AbortWithStatus()

Otherwise, caller needs to invoke WriteHeaderNow himself after
AbortWithStatus(), which is error-prone.

Also modified ErrorLoggerT() such that it always writes log to response
body. Otherwise calling AbortWithStatus() will fail to write body because
c.Writer.Written() is set true by WriteHeaderNow().
This commit is contained in:
Roy Lou 2016-04-15 00:02:29 +08:00
parent 72ffff6e44
commit 4c4444b160
5 changed files with 6 additions and 10 deletions

View File

@ -115,6 +115,7 @@ func (c *Context) Abort() {
// For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401). // For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401).
func (c *Context) AbortWithStatus(code int) { func (c *Context) AbortWithStatus(code int) {
c.Status(code) c.Status(code)
c.Writer.WriteHeaderNow()
c.Abort() c.Abort()
} }

View File

@ -545,7 +545,6 @@ func TestContextAbortWithStatus(t *testing.T) {
c, w, _ := CreateTestContext() c, w, _ := CreateTestContext()
c.index = 4 c.index = 4
c.AbortWithStatus(401) c.AbortWithStatus(401)
c.Writer.WriteHeaderNow()
assert.Equal(t, c.index, abortIndex) assert.Equal(t, c.index, abortIndex)
assert.Equal(t, c.Writer.Status(), 401) assert.Equal(t, c.Writer.Status(), 401)
@ -596,7 +595,6 @@ func TestContextTypedError(t *testing.T) {
func TestContextAbortWithError(t *testing.T) { func TestContextAbortWithError(t *testing.T) {
c, w, _ := CreateTestContext() c, w, _ := CreateTestContext()
c.AbortWithError(401, errors.New("bad input")).SetMeta("some input") c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
c.Writer.WriteHeaderNow()
assert.Equal(t, w.Code, 401) assert.Equal(t, w.Code, 401)
assert.Equal(t, c.index, abortIndex) assert.Equal(t, c.index, abortIndex)

View File

@ -28,14 +28,11 @@ func ErrorLogger() HandlerFunc {
func ErrorLoggerT(typ ErrorType) HandlerFunc { func ErrorLoggerT(typ ErrorType) HandlerFunc {
return func(c *Context) { return func(c *Context) {
c.Next() c.Next()
// avoid writting if we already wrote into the response body
if !c.Writer.Written() {
errors := c.Errors.ByType(typ) errors := c.Errors.ByType(typ)
if len(errors) > 0 { if len(errors) > 0 {
c.JSON(-1, errors) c.JSON(-1, errors)
} }
} }
}
} }
// Instances a Logger middleware that will write the logs to gin.DefaultWriter // Instances a Logger middleware that will write the logs to gin.DefaultWriter

View File

@ -116,7 +116,7 @@ func TestErrorLogger(t *testing.T) {
w = performRequest(router, "GET", "/print") w = performRequest(router, "GET", "/print")
assert.Equal(t, w.Code, 500) assert.Equal(t, w.Code, 500)
assert.Equal(t, w.Body.String(), "hola!") assert.Equal(t, w.Body.String(), "hola!{\"error\":\"this is an error\"}\n")
} }
func TestSkippingPaths(t *testing.T) { func TestSkippingPaths(t *testing.T) {

View File

@ -39,5 +39,5 @@ func TestPanicWithAbort(t *testing.T) {
// RUN // RUN
w := performRequest(router, "GET", "/recovery") w := performRequest(router, "GET", "/recovery")
// TEST // TEST
assert.Equal(t, w.Code, 500) // NOT SURE assert.Equal(t, w.Code, 400)
} }