return actual error when regex.Compile fails

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
Somtochi Onyekwere 2022-04-08 16:26:52 +01:00
parent 6477e1500e
commit b30e2458d8
2 changed files with 5 additions and 4 deletions

View File

@ -326,8 +326,8 @@ func redactTokenFromError(err error, token string, log logr.Logger) error {
re, compileErr := regexp.Compile(fmt.Sprintf("%s*", token))
if compileErr != nil {
log.Error(compileErr, "error redacting token from error message")
return err
newErrStr := fmt.Sprintf("error redacting token from error message: %s", compileErr)
return errors.New(newErrStr)
}
redacted := re.ReplaceAllString(err.Error(), "*****")

View File

@ -2,6 +2,7 @@ package server
import (
"errors"
"strings"
"testing"
"github.com/fluxcd/pkg/runtime/logger"
@ -54,7 +55,7 @@ func TestRedactTokenFromError(t *testing.T) {
name: "return error on invalid UTF-8 string",
token: "\x18\xd0\xfa\xab\xb2\x93\xbb;\xc0l\xf4\xdc",
originalErrStr: `Cannot post to github with token \x18\xd0\xfa\xab\xb2\x93\xbb;\xc0l\xf4\xdc\\n`,
expectedErrStr: `Cannot post to github with token \x18\xd0\xfa\xab\xb2\x93\xbb;\xc0l\xf4\xdc\\n`,
expectedErrStr: `error redacting token from error message`,
},
}
@ -65,7 +66,7 @@ func TestRedactTokenFromError(t *testing.T) {
t.Fatalf("error shouldn't be nil")
}
if err.Error() != tt.expectedErrStr {
if !strings.Contains(err.Error(), tt.expectedErrStr) {
t.Errorf("expected error string '%s' but got '%s'",
tt.expectedErrStr, err)
}