Skip to content

Commit

Permalink
improve gin errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jvexiau committed Sep 23, 2020
1 parent 5de8fd8 commit 824e28b
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions ginhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,20 @@ func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc {
// capture response in case of invalid response
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
c.Next()

defer func() {
panicErr := recover()
didPanic := panicErr != nil

if didPanic {
ext.Error.Set(sp, true)
}
sp.Finish()

ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status()))
if c.Writer.Status() >= http.StatusInternalServerError {
ext.Error.Set(sp, true)
if len(c.Errors) > 0 {
for _, err := range c.Errors {
ext.LogError(sp, err)
}
}
}

if opts.logResponse && c.Writer.Status() >= http.StatusBadRequest {
sp.SetTag("http.response", blw.body.String())
}
sp.Finish()

if didPanic {
panic(panicErr)
}
}()
ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status()))
if c.Writer.Status() >= http.StatusInternalServerError {
ext.Error.Set(sp, true)
}

c.Next()
if len(c.Errors) > 0 {
sp.SetTag("gin.errors", c.Errors.String())
}

if opts.logResponse && c.Writer.Status() >= http.StatusBadRequest {
sp.SetTag("http.response", blw.body.String())
}
sp.Finish()
}
}

0 comments on commit 824e28b

Please sign in to comment.