diff --git a/pkg/controller/signup_test.go b/pkg/controller/signup_test.go index f80d9e09..e968165d 100644 --- a/pkg/controller/signup_test.go +++ b/pkg/controller/signup_test.go @@ -594,7 +594,8 @@ func (s *TestSignupSuite) TestVerifyPhoneCodeHandler() { Key: "code", Value: "555555", } - rr := initPhoneVerification(s.T(), handler, param, nil, userID, "", http.MethodGet, "/api/v1/signup/verification/555555") + rr := initPhoneVerification(s.T(), handler, param, nil, userID, "", http.MethodGet, + "/api/v1/signup/verification/555555") // Check the status code is what we expect. require.Equal(s.T(), http.StatusInternalServerError, rr.Code) @@ -608,7 +609,7 @@ func (s *TestSignupSuite) TestVerifyPhoneCodeHandler() { require.Equal(s.T(), "there was an error while updating your account - please wait a moment before "+ "trying again. If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com for "+ "assistance: error while verifying phone code", bodyParams["message"]) - require.Equal(s.T(), "error while verifying phone code", bodyParams["details"]) + require.Equal(s.T(), "unexpected error while verifying phone code", bodyParams["details"]) }) s.Run("verifycode returns status error", func() { diff --git a/pkg/signup/service/signup_service.go b/pkg/signup/service/signup_service.go index 27587cdf..3605ab44 100644 --- a/pkg/signup/service/signup_service.go +++ b/pkg/signup/service/signup_service.go @@ -350,7 +350,7 @@ func (s *ServiceImpl) DoGetSignup(ctx *gin.Context, provider ResourceProvider, u var userSignup *toolchainv1alpha1.UserSignup var err error - err = pollUpdateSignup(ctx, func() error { + err = signup.PollUpdateSignup(ctx, func() error { // Retrieve UserSignup resource from the host cluster, using the specified UserID and username var getError error userSignup, getError = s.DoGetUserSignupFromIdentifier(provider, userID, username) @@ -621,30 +621,3 @@ func getAppsURL(appRouteName string, signup signup.Signup) string { appsURL := signup.ConsoleURL[index:] return fmt.Sprintf("https://%s%s", appRouteName, appsURL) } - -func pollUpdateSignup(ctx *gin.Context, updater func() error) error { - // Attempt to execute an update function, retrying a number of times if the update fails - attempts := 0 - for { - attempts++ - - // Attempt the update - updateErr := updater() - - // If there was an error, then only log it for now - if updateErr != nil { - log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts)) - } else { - // Otherwise if there was no error executing the update, then break here - break - } - - // If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual - // error to the user here, as we've already logged it - if attempts > 4 { - return updateErr - } - } - - return nil -} diff --git a/pkg/signup/signup.go b/pkg/signup/signup.go index ee74233e..38552d59 100644 --- a/pkg/signup/signup.go +++ b/pkg/signup/signup.go @@ -1,5 +1,11 @@ package signup +import ( + "fmt" + "github.com/codeready-toolchain/registration-service/pkg/log" + "github.com/gin-gonic/gin" +) + // Signup represents Signup resource which is a wrapper of K8s UserSignup // and the corresponding MasterUserRecord resources. type Signup struct { @@ -49,3 +55,32 @@ type Status struct { // Default value is false. VerificationRequired bool `json:"verificationRequired"` } + +// PollUpdateSignup will attempt to execute the provided updater function, and if it fails +// will reattempt the update for a limited number of retries +func PollUpdateSignup(ctx *gin.Context, updater func() error) error { + // Attempt to execute an update function, retrying a number of times if the update fails + attempts := 0 + for { + attempts++ + + // Attempt the update + updateErr := updater() + + // If there was an error, then only log it for now + if updateErr != nil { + log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts)) + } else { + // Otherwise if there was no error executing the update, then break here + break + } + + // If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual + // error to the user here, as we've already logged it + if attempts > 4 { + return updateErr + } + } + + return nil +} diff --git a/pkg/verification/service/verification_service.go b/pkg/verification/service/verification_service.go index 10dec3da..3853b4d6 100644 --- a/pkg/verification/service/verification_service.go +++ b/pkg/verification/service/verification_service.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "errors" "fmt" + signuppkg "github.com/codeready-toolchain/registration-service/pkg/signup" "net/http" "strconv" "time" @@ -184,9 +185,12 @@ func (s *ServiceImpl) InitVerification(ctx *gin.Context, userID, username, e164P return nil } - updateErr := pollUpdateSignup(ctx, doUpdate) + updateErr := signuppkg.PollUpdateSignup(ctx, doUpdate) if updateErr != nil { - return updateErr + log.Error(ctx, updateErr, "error updating UserSignup") + return errors.New("there was an error while updating your account - please wait a moment before " + + "trying again. If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com for " + + "assistance: error while verifying phone code") } return initError @@ -311,9 +315,12 @@ func (s *ServiceImpl) VerifyPhoneCode(ctx *gin.Context, userID, username, code s return nil } - updateErr := pollUpdateSignup(ctx, doUpdate) + updateErr := signuppkg.PollUpdateSignup(ctx, doUpdate) if updateErr != nil { - return updateErr + log.Error(ctx, updateErr, "error updating UserSignup") + return errors.New("there was an error while updating your account - please wait a moment before " + + "trying again. If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com for " + + "assistance: error while verifying phone code") } return @@ -366,7 +373,7 @@ func (s *ServiceImpl) VerifyActivationCode(ctx *gin.Context, userID, username, c return nil } - if err := pollUpdateSignup(ctx, doUpdate); err != nil { + if err := signuppkg.PollUpdateSignup(ctx, doUpdate); err != nil { log.Errorf(ctx, err, "unable to update user signup after validating activation code") } }() @@ -422,32 +429,3 @@ func checkAttempts(signup *toolchainv1alpha1.UserSignup) (int, error) { } return attemptsMade, nil } - -func pollUpdateSignup(ctx *gin.Context, updater func() error) error { - // Attempt to execute an update function, retrying a number of times if the update fails - attempts := 0 - for { - attempts++ - - // Attempt the update - updateErr := updater() - - // If there was an error, then only log it for now - if updateErr != nil { - log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts)) - } else { - // Otherwise if there was no error executing the update, then break here - break - } - - // If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual - // error to the user here, as we've already logged it - if attempts > 4 { - return crterrors.NewInternalError(errors.New("there was an error while updating your account - please wait a moment before trying again."+ - " If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com for assistance"), - "error while verifying phone code") - } - } - - return nil -} diff --git a/pkg/verification/service/verification_service_test.go b/pkg/verification/service/verification_service_test.go index 72b16ad2..93c1d95a 100644 --- a/pkg/verification/service/verification_service_test.go +++ b/pkg/verification/service/verification_service_test.go @@ -317,7 +317,7 @@ func (s *TestVerificationServiceSuite) TestInitVerificationClientFailure() { // Cause the client UPDATE call to fail always s.FakeUserSignupClient.MockUpdate = func(userSignup *toolchainv1alpha1.UserSignup) (*toolchainv1alpha1.UserSignup, error) { - return nil, errors.New("update failed") + return nil, errors.New("there was an error while updating your account - please wait a moment before trying again. If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com \"+\n\t\t\t\"for assistance: error while verifying phone code") } defer func() { s.FakeUserSignupClient.MockUpdate = nil }()