mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
refactor(recovery): smart error comparison
This commit is contained in:
parent
65639e876c
commit
6cfc3cd0fc
16
recovery.go
16
recovery.go
@ -10,12 +10,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/internal/bytesconv"
|
"github.com/gin-gonic/gin/internal/bytesconv"
|
||||||
@ -58,15 +58,9 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
// 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 isBrokenPipeOrConnReset bool
|
var isBrokenPipeOrConnReset bool
|
||||||
if ne, ok := rec.(*net.OpError); ok {
|
err, ok := rec.(error)
|
||||||
var se *os.SyscallError
|
if ok {
|
||||||
if errors.As(ne, &se) {
|
isBrokenPipeOrConnReset = errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET)
|
||||||
seStr := strings.ToLower(se.Error())
|
|
||||||
if strings.Contains(seStr, "broken pipe") ||
|
|
||||||
strings.Contains(seStr, "connection reset by peer") {
|
|
||||||
isBrokenPipeOrConnReset = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
const stackSkip = 3
|
const stackSkip = 3
|
||||||
@ -82,7 +76,7 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
}
|
}
|
||||||
if isBrokenPipeOrConnReset {
|
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(rec.(error)) //nolint: errcheck
|
c.Error(err) //nolint: errcheck
|
||||||
c.Abort()
|
c.Abort()
|
||||||
} else {
|
} else {
|
||||||
handle(c, rec)
|
handle(c, rec)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user