Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check for broken pipe errors in Gin integration #82

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package sentrygin

import (
"context"
"net"
"net/http"
"os"
"strings"
"time"

"github.com/getsentry/sentry-go"
Expand Down Expand Up @@ -63,19 +66,34 @@ func (h *handler) handle(ctx *gin.Context) {

func (h *handler) recoverWithSentry(hub *sentry.Hub, r *http.Request) {
if err := recover(); err != nil {
eventID := hub.RecoverWithContext(
context.WithValue(r.Context(), sentry.RequestContextKey, r),
err,
)
if eventID != nil && h.waitForDelivery {
hub.Flush(h.timeout)
if !isBrokenPipeError(err) {
eventID := hub.RecoverWithContext(
context.WithValue(r.Context(), sentry.RequestContextKey, r),
err,
)
if eventID != nil && h.waitForDelivery {
hub.Flush(h.timeout)
}
}
if h.repanic {
panic(err)
}
}
}

// Check for a broken connection, as this is what Gin does already
func isBrokenPipeError(err interface{}) bool {
if netErr, ok := err.(*net.OpError); ok {
if sysErr, ok := netErr.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(sysErr.Error()), "broken pipe") ||
strings.Contains(strings.ToLower(sysErr.Error()), "connection reset by peer") {
return true
}
}
}
return false
}

// GetHubFromContext retrieves attached *sentry.Hub instance from gin.Context.
func GetHubFromContext(ctx *gin.Context) *sentry.Hub {
if hub, ok := ctx.Get(valuesKey); ok {
Expand Down