Classify configuration command failures
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package ckconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
@@ -15,7 +17,7 @@ func runConfigOperation[I, S any](invocation commandkit.Invocation, operation Op
|
||||
return invocation.Help()
|
||||
}
|
||||
if err := invocation.Context.Err(); err != nil {
|
||||
return configFailure("Configuration operation was canceled.", commandkit.Explanation{}, err)
|
||||
return configFailure(invocation.Context, "Configuration operation was canceled.", commandkit.Explanation{}, err)
|
||||
}
|
||||
plan := input.plan
|
||||
var state S
|
||||
@@ -64,11 +66,23 @@ func queueConfigAcquisition(reporter *commandkit.Reporter, acquired commandkit.A
|
||||
}
|
||||
}
|
||||
|
||||
func configFailure(message string, explanation commandkit.Explanation, cause error) commandkit.Outcome {
|
||||
func configFailure(ctx context.Context, message string, explanation commandkit.Explanation, cause error) commandkit.Outcome {
|
||||
if explanation.Message != "" || len(explanation.Details) > 0 {
|
||||
message = explanation.Message
|
||||
}
|
||||
if ctx != nil {
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
if cause == nil {
|
||||
cause = canceled
|
||||
} else if !errors.Is(cause, canceled) {
|
||||
cause = errors.Join(cause, canceled)
|
||||
}
|
||||
}
|
||||
}
|
||||
outcome := commandkit.Failure(message, explanation.Details...)
|
||||
if cause != nil && ClassifyConfigurationError(cause) == commandkit.OutcomeUsage {
|
||||
outcome = commandkit.Usage(message, explanation.Details...)
|
||||
}
|
||||
if cause != nil {
|
||||
outcome = outcome.WithCause(cause)
|
||||
}
|
||||
@@ -76,6 +90,7 @@ func configFailure(message string, explanation commandkit.Explanation, cause err
|
||||
}
|
||||
|
||||
func configAcquisitionFailure(
|
||||
ctx context.Context,
|
||||
reporter *commandkit.Reporter,
|
||||
operation, scope string,
|
||||
acquired commandkit.Acquired[View],
|
||||
@@ -83,7 +98,7 @@ func configAcquisitionFailure(
|
||||
) commandkit.Outcome {
|
||||
queueConfigAcquisition(reporter, acquired)
|
||||
message := fmt.Sprintf("Unable to %s configuration scope %q.", operation, scope)
|
||||
return configFailure(message, acquired.Explanation, cause)
|
||||
return configFailure(ctx, message, acquired.Explanation, cause)
|
||||
}
|
||||
|
||||
// Preserve the preparation/delivery cancellation checkpoint for buffered reports.
|
||||
@@ -105,7 +120,7 @@ func renderPreparedConfigReport(invocation commandkit.Invocation, explanation co
|
||||
canceled = invocation.Context.Err()
|
||||
}
|
||||
if canceled != nil {
|
||||
return configFailure("Configuration report preparation was canceled.", explanation, canceled)
|
||||
return configFailure(invocation.Context, "Configuration report preparation was canceled.", explanation, canceled)
|
||||
}
|
||||
if renderErr == nil {
|
||||
_ = invocation.Reporter().Report(commandkit.Report{Value: value})
|
||||
|
||||
Reference in New Issue
Block a user