Documentation
¶
Overview ¶
Package cli provides standard error handling infrastructure for CLI entry points.
This package implements a consistent error handling pattern across all command-line tools, including:
- Panic recovery with defer/recover
- Structured error wrapping with context
- Consistent exit codes
- Error logging for debugging
Usage pattern:
func main() {
cli.Main(run)
}
func run() error {
// Actual main logic here
return nil
}
Index ¶
Constants ¶
const ( ExitSuccess = 0 ExitCodeError = 1 ExitCodeUsage = 2 ExitCodePanic = 70 // Internal software error (BSD EX_SOFTWARE) ExitCodeInterrupt = 4 )
Exit codes
Variables ¶
This section is empty.
Functions ¶
func IsUsageError ¶
IsUsageError checks if an error is a UsageError.
func Main ¶
func Main(runFn RunFunc)
Main wraps a RunFunc with standard error handling and panic recovery. It catches panics, logs errors appropriately, and exits with the correct code.
Usage:
func main() {
cli.Main(run)
}
func run() error {
// Your main logic here
return nil
}
func SanitizeError ¶
SanitizeError removes sensitive information from error messages.
func WrapContext ¶
WrapContext wraps an error with additional context information. This is useful for adding stack-like context to errors as they propagate.
Types ¶
type ExitError ¶
type ExitError struct {
// Message is the error message to display
Message string
// Code is the exit code to use
Code int
// Underlying is the original error (if any)
Underlying error
}
ExitError is a standard error that carries an exit code.
func NewExitError ¶
NewExitError creates an ExitError with a message and exit code.
func WrapExitError ¶
WrapExitError wraps an existing error with an exit code.
type RunFunc ¶
type RunFunc func() error
RunFunc is the signature for the main logic function. All CLI entry points should implement their main logic in a function of this type, then pass it to Main().
type UsageError ¶
type UsageError struct {
Message string
}
UsageError is an error indicating incorrect command-line usage. It should exit with code 2 (Unix convention for usage errors).
func NewUsageError ¶
func NewUsageError(message string) *UsageError
NewUsageError creates a UsageError.
func (*UsageError) Error ¶
func (e *UsageError) Error() string
Error implements the error interface.