Skip to content

Commit 8295ad1

Browse files
committed
refactor: buildValidateError helper to reduce complexity
golangci-lint was failing cyclomatic complexity for function executeCheck. refactored out a buildValidateError function similar to buildCanApplyError Signed-off-by: Andy Stoneberg <astonebe@redhat.com>
1 parent f1f872b commit 8295ad1

File tree

1 file changed

+66
-60
lines changed

1 file changed

+66
-60
lines changed

pkg/lint/check/executor.go

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -132,66 +132,7 @@ func (e *Executor) executeCheck(ctx context.Context, target Target, check Check)
132132

133133
// If check returned an error, create a diagnostic result with error condition
134134
if err != nil {
135-
var message string
136-
var reason string
137-
138-
// Handle specific error types
139-
switch {
140-
case apierrors.IsForbidden(err):
141-
reason = ReasonAPIAccessDenied
142-
message = "Insufficient permissions to access cluster resources"
143-
// Log to stderr if verbose
144-
if e.io != nil {
145-
e.io.Errorf("Permission denied: %s - Check: %s", message, check.Name())
146-
}
147-
case apierrors.IsUnauthorized(err):
148-
reason = ReasonAPIAccessDenied
149-
message = "Authentication required to access cluster resources"
150-
// Log to stderr if verbose
151-
if e.io != nil {
152-
e.io.Errorf("Unauthorized: %s - Check: %s", message, check.Name())
153-
}
154-
case apierrors.IsTimeout(err):
155-
reason = ReasonCheckExecutionFailed
156-
message = "Request timed out"
157-
case apierrors.IsServiceUnavailable(err) || apierrors.IsServerTimeout(err):
158-
reason = ReasonCheckExecutionFailed
159-
message = "API server is unavailable or overloaded"
160-
default:
161-
reason = ReasonCheckExecutionFailed
162-
}
163-
164-
errorResult := result.New(
165-
string(check.Group()),
166-
check.CheckKind(),
167-
check.CheckType(),
168-
check.Description(),
169-
)
170-
171-
var condition result.Condition
172-
if reason == ReasonCheckExecutionFailed && message == "" {
173-
condition = NewCondition(
174-
ConditionTypeValidated,
175-
metav1.ConditionUnknown,
176-
WithReason(reason),
177-
WithMessage("Check execution failed: %v", err),
178-
)
179-
} else {
180-
condition = NewCondition(
181-
ConditionTypeValidated,
182-
metav1.ConditionUnknown,
183-
WithReason(reason),
184-
WithMessage("%s", message),
185-
)
186-
}
187-
188-
errorResult.Status.Conditions = []result.Condition{condition}
189-
190-
return CheckExecution{
191-
Check: check,
192-
Result: errorResult,
193-
Error: err,
194-
}
135+
return e.buildValidateError(check, err)
195136
}
196137

197138
// Validate the result
@@ -224,3 +165,68 @@ func (e *Executor) executeCheck(ctx context.Context, target Target, check Check)
224165
Error: nil,
225166
}
226167
}
168+
169+
// buildValidateError creates a CheckExecution for a Validate error,
170+
// classifying the error into an appropriate reason and message.
171+
func (e *Executor) buildValidateError(check Check, err error) CheckExecution {
172+
var message string
173+
var reason string
174+
175+
// Handle specific error types
176+
switch {
177+
case apierrors.IsForbidden(err):
178+
reason = ReasonAPIAccessDenied
179+
message = "Insufficient permissions to access cluster resources"
180+
181+
if e.io != nil {
182+
e.io.Errorf("Permission denied: %s - Check: %s", message, check.Name())
183+
}
184+
case apierrors.IsUnauthorized(err):
185+
reason = ReasonAPIAccessDenied
186+
message = "Authentication required to access cluster resources"
187+
188+
if e.io != nil {
189+
e.io.Errorf("Unauthorized: %s - Check: %s", message, check.Name())
190+
}
191+
case apierrors.IsTimeout(err):
192+
reason = ReasonCheckExecutionFailed
193+
message = "Request timed out"
194+
case apierrors.IsServiceUnavailable(err) || apierrors.IsServerTimeout(err):
195+
reason = ReasonCheckExecutionFailed
196+
message = "API server is unavailable or overloaded"
197+
default:
198+
reason = ReasonCheckExecutionFailed
199+
}
200+
201+
errorResult := result.New(
202+
string(check.Group()),
203+
check.CheckKind(),
204+
check.CheckType(),
205+
check.Description(),
206+
)
207+
208+
var condition result.Condition
209+
if reason == ReasonCheckExecutionFailed && message == "" {
210+
condition = NewCondition(
211+
ConditionTypeValidated,
212+
metav1.ConditionUnknown,
213+
WithReason(reason),
214+
WithMessage("Check execution failed: %v", err),
215+
)
216+
} else {
217+
condition = NewCondition(
218+
ConditionTypeValidated,
219+
metav1.ConditionUnknown,
220+
WithReason(reason),
221+
WithMessage("%s", message),
222+
)
223+
}
224+
225+
errorResult.Status.Conditions = []result.Condition{condition}
226+
227+
return CheckExecution{
228+
Check: check,
229+
Result: errorResult,
230+
Error: err,
231+
}
232+
}

0 commit comments

Comments
 (0)