Skip to content

Commit

Permalink
reuse compiled regex instead of recreating
Browse files Browse the repository at this point in the history
  • Loading branch information
frodopwns committed Jul 8, 2020
1 parent be2cdcc commit df48da7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/errhelp/errhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"strings"
)

// ErrIdsRegex is used to find and remove uuids from errors
var ErrIdsRegex *regexp.Regexp

// ErrTimesRegex allows timestamp seconds to be removed from error strings
var ErrTimesRegex *regexp.Regexp

// StripErrorIDs takes an error and returns its string representation after filtering some common ID patterns
func StripErrorIDs(err error) string {
patterns := []string{
Expand All @@ -17,14 +23,20 @@ func StripErrorIDs(err error) string {
"Tracking ID: ",
"requestId",
}
reg := regexp.MustCompile(fmt.Sprintf(`(%s)\S+`, strings.Join(patterns, "|")))
return reg.ReplaceAllString(err.Error(), "")

if ErrIdsRegex == nil {
ErrIdsRegex = regexp.MustCompile(fmt.Sprintf(`(%s)\S+`, strings.Join(patterns, "|")))
}

return ErrIdsRegex.ReplaceAllString(err.Error(), "")

}

// StripErrorTimes removes the hours:minutes:seconds from a date to prevent updates to Status.Message from changing unnecessarily
func StripErrorTimes(err string) string {
reg := regexp.MustCompile(`(T\d\d:\d\d:\d\d)\"`)
return reg.ReplaceAllString(err, "")
if ErrTimesRegex == nil {
ErrTimesRegex = regexp.MustCompile(`(T\d\d:\d\d:\d\d)\"`)
}
return ErrTimesRegex.ReplaceAllString(err, "")

}

0 comments on commit df48da7

Please sign in to comment.