mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
refactor(recovery): rename var in CustomRecoveryWithWriter
This commit is contained in:
parent
9968c4bf9d
commit
65639e876c
22
recovery.go
22
recovery.go
@ -54,38 +54,38 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
}
|
}
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if rec := recover(); rec != nil {
|
||||||
// Check for a broken connection, as it is not really a
|
// Check for a broken connection, as it is not really a
|
||||||
// condition that warrants a panic stack trace.
|
// condition that warrants a panic stack trace.
|
||||||
var brokenPipe bool
|
var isBrokenPipeOrConnReset bool
|
||||||
if ne, ok := err.(*net.OpError); ok {
|
if ne, ok := rec.(*net.OpError); ok {
|
||||||
var se *os.SyscallError
|
var se *os.SyscallError
|
||||||
if errors.As(ne, &se) {
|
if errors.As(ne, &se) {
|
||||||
seStr := strings.ToLower(se.Error())
|
seStr := strings.ToLower(se.Error())
|
||||||
if strings.Contains(seStr, "broken pipe") ||
|
if strings.Contains(seStr, "broken pipe") ||
|
||||||
strings.Contains(seStr, "connection reset by peer") {
|
strings.Contains(seStr, "connection reset by peer") {
|
||||||
brokenPipe = true
|
isBrokenPipeOrConnReset = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
const stackSkip = 3
|
const stackSkip = 3
|
||||||
if brokenPipe {
|
if isBrokenPipeOrConnReset {
|
||||||
logger.Printf("%s\n%s%s", err, secureRequestDump(c.Request), reset)
|
logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), reset)
|
||||||
} else if IsDebugging() {
|
} else if IsDebugging() {
|
||||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
|
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
|
||||||
timeFormat(time.Now()), secureRequestDump(c.Request), err, stack(stackSkip), reset)
|
timeFormat(time.Now()), secureRequestDump(c.Request), rec, stack(stackSkip), reset)
|
||||||
} else {
|
} else {
|
||||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",
|
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",
|
||||||
timeFormat(time.Now()), err, stack(stackSkip), reset)
|
timeFormat(time.Now()), rec, stack(stackSkip), reset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if brokenPipe {
|
if isBrokenPipeOrConnReset {
|
||||||
// If the connection is dead, we can't write a status to it.
|
// If the connection is dead, we can't write a status to it.
|
||||||
c.Error(err.(error)) //nolint: errcheck
|
c.Error(rec.(error)) //nolint: errcheck
|
||||||
c.Abort()
|
c.Abort()
|
||||||
} else {
|
} else {
|
||||||
handle(c, err)
|
handle(c, rec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user