twitch-bot/internal/helpers/operr.go
Knut Ahlers 49aba55cfe
[core] Clean IPs from socket errors
in order not to log IPs (GDPR) and also to prevent spread of errors in
Sentry logging as IP/Port combinations are unique per connection and
therefore will spam with many unique errors. By removing the unique
information from the error the errors should be groupable again.

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-06-24 14:59:36 +02:00

35 lines
1.1 KiB
Go

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(), ""))
}