diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go new file mode 100644 index 0000000..7b43c6d --- /dev/null +++ b/internal/helpers/helpers.go @@ -0,0 +1,2 @@ +// Package helpers contains helpers to assist in other parts of the code +package helpers diff --git a/internal/helpers/operr.go b/internal/helpers/operr.go new file mode 100644 index 0000000..5ac52c3 --- /dev/null +++ b/internal/helpers/operr.go @@ -0,0 +1,35 @@ +package helpers + +import ( + "errors" + "net" + "regexp" +) + +// CleanOpError checks whether a *net.OpError is included in the error +// and if so removes the included address information. This can happen +// in two ways: If the passed error is indeed an OpError the address +// info is just patched out. If the OpError is buried deeper inside +// the wrapped error stack, a new error with patched message is created +// sacrificing the wrapping and possible included stacktrace. +// +// As of the loss of information this is only intended to clean up +// logging and not be used in error returns. +func CleanOpError(err error) error { + if opE, ok := err.(*net.OpError); ok { + // Error in the outmost position is an OpError, lets just patch it + opE.Source = nil + opE.Addr = nil + return opE + } + + var opE *net.OpError + if !errors.As(err, &opE) { + // There is no OpError somewhere inside, keep the error as is + return err + } + + // Patch out IP information and create an new error with its message + return errors.New(regexp.MustCompile(` (?:(?:[0-9]+\.){3}[0-9]+:[0-9]+(?:->)?)+`). + ReplaceAllString(err.Error(), "")) +} diff --git a/main.go b/main.go index b170841..321e7d0 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/Luzifer/go_helpers/v2/str" "github.com/Luzifer/rconfig/v2" + "github.com/Luzifer/twitch-bot/v3/internal/helpers" "github.com/Luzifer/twitch-bot/v3/internal/service/access" "github.com/Luzifer/twitch-bot/v3/internal/service/timer" "github.com/Luzifer/twitch-bot/v3/pkg/database" @@ -281,13 +282,7 @@ func main() { go func() { log.Info("(re-)connecting IRC client") if err := ircHdl.Run(); err != nil { - log.WithError(err).Debug("IRC connection failed") - // We don't want to spam Sentry with errors each being unique - // and not groupable as it contains `localip:localport -> remoteip:remoteport` - // therefore we replace it with a generic error and just put - // the underlying error in the debug-level log which doesn't - // get sent to Sentry - log.WithError(errors.New("connection to Twitch IRC servers broke")).Error("IRC run exited unexpectedly") + log.WithError(helpers.CleanOpError(err)).Error("IRC run exited unexpectedly") } time.Sleep(ircReconnectDelay) ircDisconnected <- struct{}{} diff --git a/pkg/twitch/eventsubWebsocketClient.go b/pkg/twitch/eventsubWebsocketClient.go index ab47082..54989b2 100644 --- a/pkg/twitch/eventsubWebsocketClient.go +++ b/pkg/twitch/eventsubWebsocketClient.go @@ -12,6 +12,8 @@ import ( "github.com/gorilla/websocket" "github.com/pkg/errors" "github.com/sirupsen/logrus" + + "github.com/Luzifer/twitch-bot/v3/internal/helpers" ) const ( @@ -173,7 +175,7 @@ func (e *EventSubSocketClient) Run() error { defer func() { if err := e.conn.Close(); err != nil { - e.logger.WithError(err).Error("finally closing socket") + e.logger.WithError(helpers.CleanOpError(err)).Error("finally closing socket") } }() diff --git a/twitchWatcher.go b/twitchWatcher.go index 6bff3e8..3b2429e 100644 --- a/twitchWatcher.go +++ b/twitchWatcher.go @@ -7,6 +7,7 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "github.com/Luzifer/twitch-bot/v3/internal/helpers" "github.com/Luzifer/twitch-bot/v3/internal/service/access" "github.com/Luzifer/twitch-bot/v3/pkg/twitch" "github.com/Luzifer/twitch-bot/v3/plugins" @@ -392,7 +393,7 @@ func (t *twitchWatcher) updateChannelFromAPI(channel string) error { log.WithField("channel", channel).Info("watching for eventsub events") go func(storedStatus *twitchChannelState) { if err := storedStatus.esc.Run(); err != nil { - log.WithField("channel", channel).WithError(err).Error("eventsub client caused error") + log.WithField("channel", channel).WithError(helpers.CleanOpError(err)).Error("eventsub client caused error") } storedStatus.CloseESC() }(storedStatus)