Skip to content

Commit

Permalink
Update error enrichment in Condition step to return original error if…
Browse files Browse the repository at this point in the history
… condition function is unknown (Azure#3046)
  • Loading branch information
tsatam authored Jul 19, 2023
1 parent 4d57b4b commit 39fb55d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/util/steps/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ func (c conditionStep) run(ctx context.Context, log *logrus.Entry) error {
return nil
}
if errors.Is(err, wait.ErrWaitTimeout) {
return enrichConditionTimeoutError(c.f)
return enrichConditionTimeoutError(c.f, err)
}
return err
}

// Instead of giving Generic, timed out waiting for a condition, error
// returns enriched error messages mentioned in timeoutConditionErrors
func enrichConditionTimeoutError(f conditionFunction) error {
func enrichConditionTimeoutError(f conditionFunction, originalErr error) error {
funcNameParts := strings.Split(FriendlyName(f), ".")
funcName := strings.TrimSuffix(funcNameParts[len(funcNameParts)-1], "-fm")

message, exists := timeoutConditionErrors[funcName]
if !exists {
return errors.New("timed out waiting for the condition")
return originalErr
}
return api.NewCloudError(
http.StatusInternalServerError,
Expand Down
17 changes: 10 additions & 7 deletions pkg/util/steps/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package steps

import (
"context"
"errors"
"testing"
)

Expand All @@ -23,17 +24,19 @@ func hiveClusterInstallationComplete(context.Context) (bool, error) { ret

func TestEnrichConditionTimeoutError(t *testing.T) {
for _, tt := range []struct {
desc string
function conditionFunction
wantErr string
desc string
function conditionFunction
originalErr string
wantErr string
}{
// Verify response for func's mention in timeoutConditionErrors and
// Emit generic Error if an unknown func
{
// unknown function
desc: "test conditionfail for func - unknownFunc",
function: timingOutCondition,
wantErr: "timed out waiting for the condition",
desc: "test conditionfail for func - unknownFunc",
function: timingOutCondition,
originalErr: "timed out waiting for the condition",
wantErr: "timed out waiting for the condition",
},
{
desc: "test conditionfail for func - apiServersReady",
Expand Down Expand Up @@ -87,7 +90,7 @@ func TestEnrichConditionTimeoutError(t *testing.T) {
},
} {
t.Run(tt.desc, func(t *testing.T) {
if got := enrichConditionTimeoutError(tt.function); got.Error() != tt.wantErr {
if got := enrichConditionTimeoutError(tt.function, errors.New(tt.originalErr)); got.Error() != tt.wantErr {
t.Errorf("invlaid enrichConditionTimeoutError: %s, got: %s", tt.wantErr, got)
}
})
Expand Down

0 comments on commit 39fb55d

Please sign in to comment.