Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors -> rename func and extend tests #1496

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/edp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (e edpError) Code() int {
return e.code
}

func (e edpError) GetDependency() kebError.Component {
func (e edpError) GetComponent() kebError.Component {
return kebError.EDPDependency
}

Expand Down
12 changes: 6 additions & 6 deletions internal/error/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Component string
type ErrorReporter interface {
error
GetReason() Reason
GetDependency() Component
GetComponent() Component
}

// error reporter
Expand Down Expand Up @@ -57,7 +57,7 @@ func (err LastError) GetReason() Reason {
return err.Reason
}

func (err LastError) GetDependency() Component {
func (err LastError) GetComponent() Component {
return err.Component
}

Expand All @@ -80,12 +80,12 @@ func (err LastError) SetMessage(msg string) LastError {
return err
}

func (err LastError) StepName() string {
func (err LastError) GetStep() string {
return err.Step
}

func (err LastError) SetStepName(stepName string) LastError {
err.Step = stepName
func (err LastError) SetStep(step string) LastError {
err.Step = step
return err
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func ReasonForError(err error, step string) LastError {
return LastError{
Message: err.Error(),
Reason: status.GetReason(),
Component: status.GetDependency(),
Component: status.GetComponent(),
Step: step,
}
}
Expand Down
60 changes: 36 additions & 24 deletions internal/error/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@ func TestLastError(t *testing.T) {
expectTimeoutMsg := "something: operation has reached the time limit: 2h"

// when
edpLastErr := kebError.ReasonForError(edpErr, "")
edpConfLastErr := kebError.ReasonForError(edpConfErr, "")
dbLastErr := kebError.ReasonForError(dbErr, "")
timeoutLastErr := kebError.ReasonForError(timeoutErr, "")
edpLastErr := kebError.ReasonForError(edpErr, "s1")
edpConfLastErr := kebError.ReasonForError(edpConfErr, "s2")
dbLastErr := kebError.ReasonForError(dbErr, "s3")
timeoutLastErr := kebError.ReasonForError(timeoutErr, "s4")

// then
assert.Equal(t, edp.ErrEDPBadRequest, edpLastErr.GetReason())
assert.Equal(t, kebError.EDPDependency, edpLastErr.GetDependency())
assert.Equal(t, kebError.EDPDependency, edpLastErr.GetComponent())
assert.Equal(t, expectEdpMsg, edpLastErr.Error())
assert.Equal(t, "s1", edpLastErr.Step)

assert.Equal(t, edp.ErrEDPConflict, edpConfLastErr.GetReason())
assert.Equal(t, kebError.EDPDependency, edpConfLastErr.GetDependency())
assert.Equal(t, kebError.EDPDependency, edpConfLastErr.GetComponent())
assert.Equal(t, expectEdpConfMsg, edpConfLastErr.Error())
assert.True(t, edp.IsConflictError(edpConfErr))
assert.Equal(t, "s2", edpConfLastErr.Step)

assert.Equal(t, dberr.ErrDBNotFound, dbLastErr.GetReason())
assert.Equal(t, kebError.KebDbDependency, dbLastErr.GetDependency())
assert.Equal(t, kebError.KebDbDependency, dbLastErr.GetComponent())
assert.Equal(t, expectDbErr, dbLastErr.Error())
assert.Equal(t, "s3", dbLastErr.Step)

assert.Equal(t, kebError.KEBTimeOutCode, timeoutLastErr.GetReason())
assert.Equal(t, kebError.KEBDependency, timeoutLastErr.GetDependency())
assert.Equal(t, kebError.KEBDependency, timeoutLastErr.GetComponent())
assert.Equal(t, expectTimeoutMsg, timeoutLastErr.Error())
assert.Equal(t, "s4", timeoutLastErr.Step)
})
}

Expand All @@ -68,19 +72,21 @@ func TestTemporaryErrorToLastError(t *testing.T) {
expectEdpMsg := fmt.Sprintf("EDP server returns failed status %s", "501")

// when
lastErr := kebError.ReasonForError(tempErr, "")
edpLastErr := kebError.ReasonForError(edpTempErr, "")
lastErr := kebError.ReasonForError(tempErr, "s1")
edpLastErr := kebError.ReasonForError(edpTempErr, "s2")

// then
assert.Equal(t, kebError.HttpStatusCode, lastErr.GetReason())
assert.Equal(t, kebError.ReconcileDependency, lastErr.GetDependency())
assert.Equal(t, kebError.ReconcileDependency, lastErr.GetComponent())
assert.Equal(t, expectMsg, lastErr.Error())
assert.True(t, kebError.IsTemporaryError(tempErr))
assert.Equal(t, "s1", lastErr.Step)

assert.Equal(t, edp.ErrEDPTimeout, edpLastErr.GetReason())
assert.Equal(t, kebError.EDPDependency, edpLastErr.GetDependency())
assert.Equal(t, kebError.EDPDependency, edpLastErr.GetComponent())
assert.Equal(t, expectEdpMsg, edpLastErr.Error())
assert.True(t, kebError.IsTemporaryError(edpTempErr))
assert.Equal(t, "s2", edpLastErr.Step)
})

t.Run("new temporary error", func(t *testing.T) {
Expand All @@ -89,13 +95,14 @@ func TestTemporaryErrorToLastError(t *testing.T) {
expectMsg := "something: temporary error..."

// when
lastErr := kebError.ReasonForError(tempErr, "")
lastErr := kebError.ReasonForError(tempErr, "s1")

// then
assert.Equal(t, kebError.KEBInternalCode, lastErr.GetReason())
assert.Equal(t, kebError.KEBDependency, lastErr.GetDependency())
assert.Equal(t, kebError.KEBDependency, lastErr.GetComponent())
assert.Equal(t, expectMsg, lastErr.Error())
assert.True(t, kebError.IsTemporaryError(tempErr))
assert.Equal(t, "s1", lastErr.Step)
})
}

Expand All @@ -104,13 +111,14 @@ func TestNotFoundError(t *testing.T) {
err := fmt.Errorf("something: %w", kebError.NotFoundError{})

// when
lastErr := kebError.ReasonForError(err, "")
lastErr := kebError.ReasonForError(err, "s2")

// then
assert.EqualError(t, lastErr, "something: not found")
assert.Equal(t, kebError.ClusterNotFoundCode, lastErr.GetReason())
assert.Equal(t, kebError.ReconcileDependency, lastErr.GetDependency())
assert.Equal(t, kebError.ReconcileDependency, lastErr.GetComponent())
assert.True(t, kebError.IsNotFoundError(err))
assert.Equal(t, "s2", lastErr.Step)
}

func TestK8SLastError(t *testing.T) {
Expand All @@ -121,25 +129,29 @@ func TestK8SLastError(t *testing.T) {
errNoMatch := fmt.Errorf("something: %w", &apierr2.NoKindMatchError{})

// when
lastErrBadReq := kebError.ReasonForError(errBadReq, "")
lastErrUnexpObj := kebError.ReasonForError(errUnexpObj, "")
lastErrAmbi := kebError.ReasonForError(errAmbi, "")
lastErrNoMatch := kebError.ReasonForError(errNoMatch, "")
lastErrBadReq := kebError.ReasonForError(errBadReq, "s1")
lastErrUnexpObj := kebError.ReasonForError(errUnexpObj, "s2")
lastErrAmbi := kebError.ReasonForError(errAmbi, "s3")
lastErrNoMatch := kebError.ReasonForError(errNoMatch, "s4")

// then
assert.EqualError(t, lastErrBadReq, "something: bad request here")
assert.Equal(t, kebError.Reason("BadRequest"), lastErrBadReq.GetReason())
assert.Equal(t, kebError.K8sDependency, lastErrBadReq.GetDependency())
assert.Equal(t, kebError.K8sDependency, lastErrBadReq.GetComponent())
assert.Equal(t, "s1", lastErrBadReq.Step)

assert.ErrorContains(t, lastErrUnexpObj, "something: unexpected object: ")
assert.Equal(t, kebError.K8SUnexpectedObjectCode, lastErrUnexpObj.GetReason())
assert.Equal(t, kebError.K8sDependency, lastErrUnexpObj.GetDependency())
assert.Equal(t, kebError.K8sDependency, lastErrUnexpObj.GetComponent())
assert.Equal(t, "s2", lastErrUnexpObj.Step)

assert.ErrorContains(t, lastErrAmbi, "matches multiple resources or kinds")
assert.Equal(t, kebError.K8SAmbiguousCode, lastErrAmbi.GetReason())
assert.Equal(t, kebError.K8sDependency, lastErrAmbi.GetDependency())
assert.Equal(t, kebError.K8sDependency, lastErrAmbi.GetComponent())
assert.Equal(t, "s3", lastErrAmbi.Step)

assert.ErrorContains(t, lastErrNoMatch, "something: no matches for kind")
assert.Equal(t, kebError.K8SNoMatchCode, lastErrNoMatch.GetReason())
assert.Equal(t, kebError.K8sDependency, lastErrNoMatch.GetDependency())
assert.Equal(t, kebError.K8sDependency, lastErrNoMatch.GetComponent())
assert.Equal(t, "s4", lastErrNoMatch.Step)
}
2 changes: 1 addition & 1 deletion internal/error/not_found_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (NotFoundError) GetReason() Reason {
return ClusterNotFoundCode
}

func (NotFoundError) GetDependency() Component {
func (NotFoundError) GetComponent() Component {
return ReconcileDependency
}

Expand Down
12 changes: 6 additions & 6 deletions internal/error/temporary_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func AsTemporaryError(err error, context string, args ...interface{}) *Temporary
return &TemporaryError{message: msg}
}

func (te TemporaryError) Error() string { return te.message }
func (TemporaryError) Temporary() bool { return true }
func (TemporaryError) GetReason() Reason { return KEBInternalCode }
func (TemporaryError) GetDependency() Component { return KEBDependency }
func (te TemporaryError) Error() string { return te.message }
func (TemporaryError) Temporary() bool { return true }
func (TemporaryError) GetReason() Reason { return KEBInternalCode }
func (TemporaryError) GetComponent() Component { return KEBDependency }

func IsTemporaryError(err error) bool {
cause := UnwrapAll(err)
Expand Down Expand Up @@ -55,6 +55,6 @@ func (wte WrapTemporaryError) GetReason() Reason {
return ReasonForError(wte.err, NotSet).GetReason()
}

func (wte WrapTemporaryError) GetDependency() Component {
return ReasonForError(wte.err, NotSet).GetDependency()
func (wte WrapTemporaryError) GetComponent() Component {
return ReasonForError(wte.err, NotSet).GetComponent()
}
2 changes: 1 addition & 1 deletion internal/metricsv2/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func GetLabels(op internal.Operation) map[string]string {
labels["plan_id"] = op.ProvisioningParameters.PlanID
labels["type"] = string(op.Type)
labels["state"] = string(op.State)
labels["error_category"] = string(op.LastError.GetDependency())
labels["error_category"] = string(op.LastError.GetComponent())
labels["error_reason"] = string(op.LastError.GetReason())
labels["error"] = op.LastError.Error()
return labels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func (s *CheckRuntimeRemovalStep) Run(operation internal.Operation, log logrus.F
case gqlschema.OperationStatePending:
return operation, 30 * time.Second, nil
case gqlschema.OperationStateFailed:
lastErr := provisioner.OperationStatusLastError(status.LastError)
lastErr := provisioner.OperationStatusLastError(status.LastError, s.Name())
return s.operationManager.OperationFailed(operation, "provisioner client returns failed status", lastErr, log)
}

lastErr := provisioner.OperationStatusLastError(status.LastError)
lastErr := provisioner.OperationStatusLastError(status.LastError, s.Name())
return s.operationManager.OperationFailed(operation, fmt.Sprintf("unsupported provisioner client status: %s", status.State.String()), lastErr, log)
}
20 changes: 10 additions & 10 deletions internal/process/operation_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func Test_OperationManager_LastError(t *testing.T) {
err := operations.InsertOperation(op)
require.NoError(t, err)
op, _, err = opManager.OperationFailed(op, "friendly message", fmt.Errorf("technical err"), fixLogger())
assert.EqualValues(t, "provisioner", op.LastError.GetDependency())
assert.EqualValues(t, "provisioner", op.LastError.GetComponent())
assert.EqualValues(t, "technical err", op.LastError.Error())
assert.EqualValues(t, "friendly message", op.LastError.GetReason())
assert.EqualValues(t, "some_step", op.LastError.StepName())
assert.EqualValues(t, "some_step", op.LastError.GetStep())
})

t.Run("when all last error field set with 1 components v2", func(t *testing.T) {
Expand All @@ -103,10 +103,10 @@ func Test_OperationManager_LastError(t *testing.T) {
err := operations.InsertOperation(op)
require.NoError(t, err)
op, _, err = opManager.OperationFailed(op, "friendly message", fmt.Errorf("technical err"), fixLogger())
assert.EqualValues(t, "db - keb", op.LastError.GetDependency())
assert.EqualValues(t, "db - keb", op.LastError.GetComponent())
assert.EqualValues(t, "technical err", op.LastError.Error())
assert.EqualValues(t, "friendly message", op.LastError.GetReason())
assert.EqualValues(t, "some_step", op.LastError.StepName())
assert.EqualValues(t, "some_step", op.LastError.GetStep())
})

t.Run("when no error passed", func(t *testing.T) {
Expand All @@ -117,10 +117,10 @@ func Test_OperationManager_LastError(t *testing.T) {
err := operations.InsertOperation(op)
require.NoError(t, err)
op, _, err = opManager.OperationFailed(op, "friendly message", nil, fixLogger())
assert.EqualValues(t, "", op.LastError.GetDependency())
assert.EqualValues(t, "", op.LastError.GetComponent())
assert.EqualValues(t, "", op.LastError.Error())
assert.EqualValues(t, "", op.LastError.GetReason())
assert.EqualValues(t, "", op.LastError.StepName())
assert.EqualValues(t, "", op.LastError.GetStep())
})

t.Run("when no description passed", func(t *testing.T) {
Expand All @@ -131,10 +131,10 @@ func Test_OperationManager_LastError(t *testing.T) {
err := operations.InsertOperation(op)
require.NoError(t, err)
op, _, err = opManager.OperationFailed(op, "", fmt.Errorf("technical err"), fixLogger())
assert.EqualValues(t, "provisioner", op.LastError.GetDependency())
assert.EqualValues(t, "provisioner", op.LastError.GetComponent())
assert.EqualValues(t, "technical err", op.LastError.Error())
assert.EqualValues(t, "", op.LastError.GetReason())
assert.EqualValues(t, "some_step", op.LastError.StepName())
assert.EqualValues(t, "some_step", op.LastError.GetStep())
})

t.Run("when no description and no err passed", func(t *testing.T) {
Expand All @@ -145,10 +145,10 @@ func Test_OperationManager_LastError(t *testing.T) {
err := operations.InsertOperation(op)
require.NoError(t, err)
op, _, err = opManager.OperationFailed(op, "", nil, fixLogger())
assert.EqualValues(t, "", op.LastError.GetDependency())
assert.EqualValues(t, "", op.LastError.GetComponent())
assert.EqualValues(t, "", op.LastError.Error())
assert.EqualValues(t, "", op.LastError.GetReason())
assert.EqualValues(t, "", op.LastError.StepName())
assert.EqualValues(t, "", op.LastError.GetStep())
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/process/provisioning/check_runtime_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func (s *CheckRuntimeStep) checkRuntimeStatus(operation internal.Operation, log
case gqlschema.OperationStatePending:
return operation, 20 * time.Second, nil
case gqlschema.OperationStateFailed:
lastErr := provisioner.OperationStatusLastError(status.LastError)
lastErr := provisioner.OperationStatusLastError(status.LastError, s.Name())
return s.operationManager.OperationFailed(operation, "provisioner client returns failed status", lastErr, log)
}

lastErr := provisioner.OperationStatusLastError(status.LastError)
lastErr := provisioner.OperationStatusLastError(status.LastError, s.Name())
return s.operationManager.OperationFailed(operation, fmt.Sprintf("unsupported provisioner client status: %s", status.State.String()), lastErr, log)
}
6 changes: 3 additions & 3 deletions internal/process/staged_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (m *StagedManager) runStep(step Step, operation internal.Operation, logger
processedOperation, backoff, err = step.Run(processedOperation, stepLogger)
if err != nil {
processedOperation.LastError = kebError.ReasonForError(err, step.Name())
logOperation := stepLogger.WithFields(logrus.Fields{"error_component": processedOperation.LastError.GetDependency(), "error_reason": processedOperation.LastError.GetReason()})
logOperation := stepLogger.WithFields(logrus.Fields{"error_component": processedOperation.LastError.GetComponent(), "error_reason": processedOperation.LastError.GetReason()})
logOperation.Warnf("Last error from step: %s", processedOperation.LastError.Error())
// only save to storage, skip for alerting if error
_, err = m.operationStorage.UpdateOperation(processedOperation)
Expand All @@ -259,7 +259,7 @@ func (m *StagedManager) runStep(step Step, operation internal.Operation, logger
// - the loop takes too much time (to not block the worker too long)
if backoff == 0 || err != nil || time.Since(begin) > m.cfg.MaxStepProcessingTime {
if err != nil {
logOperation := m.log.WithFields(logrus.Fields{"step": step.Name(), "operation": processedOperation.ID, "error_component": processedOperation.LastError.GetDependency(), "error_reason": processedOperation.LastError.GetReason()})
logOperation := m.log.WithFields(logrus.Fields{"step": step.Name(), "operation": processedOperation.ID, "error_component": processedOperation.LastError.GetComponent(), "error_reason": processedOperation.LastError.GetReason()})
logOperation.Errorf("Last Error that terminated the step: %s", processedOperation.LastError.Error())
}
return processedOperation, backoff, err
Expand All @@ -270,7 +270,7 @@ func (m *StagedManager) runStep(step Step, operation internal.Operation, logger
}

func (m *StagedManager) publishEventOnFail(operation *internal.Operation, err error) {
logOperation := m.log.WithFields(logrus.Fields{"operation": operation.ID, "error_component": operation.LastError.GetDependency(), "error_reason": operation.LastError.GetReason()})
logOperation := m.log.WithFields(logrus.Fields{"operation": operation.ID, "error_component": operation.LastError.GetComponent(), "error_reason": operation.LastError.GetReason()})
logOperation.Errorf("Last error: %s", operation.LastError.Error())

m.publishOperationFinishedEvent(*operation)
Expand Down
6 changes: 3 additions & 3 deletions internal/provisioner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ func isNotFoundError(err error) bool {
return false
}

func OperationStatusLastError(lastErr *schema.LastError) kebError.ErrorReporter {
func OperationStatusLastError(lastErr *schema.LastError, step string) kebError.ErrorReporter {
var err kebError.LastError

if lastErr == nil {
return err.SetReason(kebError.ProvisionerCode).SetComponent(kebError.ProvisionerDependency).SetStepName(kebError.NotSet)
return err.SetReason(kebError.ProvisionerCode).SetComponent(kebError.ProvisionerDependency).SetStep(step)
}

return err.SetMessage(lastErr.ErrMessage).SetReason(kebError.Reason(lastErr.Reason)).SetComponent(kebError.Component(lastErr.Component)).SetStepName(kebError.NotSet)
return err.SetMessage(lastErr.ErrMessage).SetReason(kebError.Reason(lastErr.Reason)).SetComponent(kebError.Component(lastErr.Component)).SetStep(step)
}
Loading
Loading