mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-16 00:01:25 +08:00
refactor(recovery): smart error comparison
This commit is contained in:
parent
8b20252756
commit
7ba35363a3
21
recovery.go
21
recovery.go
@ -12,12 +12,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"
|
||||||
@ -61,18 +61,11 @@ 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 isBrokenPipe bool
|
var isBrokenPipe bool
|
||||||
if ne, ok := rec.(*net.OpError); ok {
|
err, ok := rec.(error)
|
||||||
var se *os.SyscallError
|
if ok {
|
||||||
if errors.As(ne, &se) {
|
isBrokenPipe = errors.Is(err, syscall.EPIPE) ||
|
||||||
seStr := strings.ToLower(se.Error())
|
errors.Is(err, syscall.ECONNRESET) ||
|
||||||
if strings.Contains(seStr, "broken pipe") ||
|
errors.Is(err, http.ErrAbortHandler)
|
||||||
strings.Contains(seStr, "connection reset by peer") {
|
|
||||||
isBrokenPipe = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if e, ok := rec.(error); ok && errors.Is(e, http.ErrAbortHandler) {
|
|
||||||
isBrokenPipe = true
|
|
||||||
}
|
}
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
if isBrokenPipe {
|
if isBrokenPipe {
|
||||||
@ -87,7 +80,7 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
}
|
}
|
||||||
if isBrokenPipe {
|
if isBrokenPipe {
|
||||||
// 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