Documentation
¶
Overview ¶
Package errorhandler centralizes Cobra command execution with KSail's error formatting rules.
This package provides an Executor that coordinates Cobra command execution, capturing stderr output and surfacing aggregated errors with proper formatting and normalization for user-friendly error messages.
The executor intercepts Cobra's error stream, applies normalization rules (such as removing redundant "Error:" prefixes), and wraps the result in a CommandError that preserves both the formatted message and the original error for proper error chain semantics.
Example usage:
// Create an executor with default normalizer
executor := errorhandler.NewExecutor()
err := executor.Execute(rootCmd)
if err != nil {
// Error is a *CommandError with normalized message
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create an executor with custom normalizer
customNormalizer := &MyNormalizer{}
executor := errorhandler.NewExecutor(
// default normalizer
)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CommandError ¶
type CommandError struct {
// contains filtered or unexported fields
}
CommandError represents a Cobra execution failure augmented with normalized stderr output.
func (*CommandError) Error ¶
func (e *CommandError) Error() string
Error implements the error interface.
func (*CommandError) Unwrap ¶
func (e *CommandError) Unwrap() error
Unwrap exposes the underlying cause for errors.Is/errors.As consumers.
type DefaultNormalizer ¶
type DefaultNormalizer struct{}
DefaultNormalizer implements Normalizer with the same semantics previously embedded in root.go.
func (DefaultNormalizer) Normalize ¶
func (DefaultNormalizer) Normalize(raw string) string
Normalize trims whitespace, removes redundant "Error:" prefixes, and preserves multi-line usage hints.
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor coordinates Cobra execution, capturing stderr output and surfacing aggregated errors.
func (*Executor) Execute ¶
Execute runs the provided command while intercepting Cobra's error stream. It returns nil on success, or a *CommandError containing both the normalized message and the original error to preserve error-chain semantics.
The function captures stderr output during command execution and applies normalization to produce user-friendly error messages.