Documentation
¶
Overview ¶
Package errors provides error handling conventions for the aix CLI.
This package defines sentinel errors for common failure conditions, an ExitError type for CLI exit code handling, and exit code constants following standard Unix conventions.
Sentinel Errors ¶
Sentinel errors allow callers to check for specific error conditions using errors.Is:
if errors.Is(err, aixerrors.ErrNotFound) {
// handle not found case
}
Exit Codes ¶
The package defines standard exit codes for CLI applications:
- ExitSuccess (0): Command completed successfully
- ExitUser (1): User-related error (invalid input, configuration, etc.)
- ExitSystem (2): System-related error (I/O, network, permissions, etc.)
ExitError ¶
ExitError wraps an underlying error with an exit code and optional suggestion for CLI applications. It supports error unwrapping via errors.Unwrap and errors.As:
err := aixerrors.NewUserError(aixerrors.ErrInvalidConfig, "Check your config file")
var exitErr *aixerrors.ExitError
if errors.As(err, &exitErr) {
if exitErr.Suggestion != "" {
fmt.Println("Suggestion:", exitErr.Suggestion)
}
os.Exit(exitErr.Code)
}
Index ¶
- Constants
- Variables
- func As(err error, target interface{}) bool
- func Cause(err error) error
- func Is(err, target error) bool
- func Join(errs ...error) error
- func New(msg string) error
- func Newf(format string, args ...interface{}) error
- func WithDetail(err error, msg string) error
- func WithDetailf(err error, format string, args ...interface{}) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type ExitError
Constants ¶
const ( // ExitSuccess indicates the command completed successfully. ExitSuccess = 0 // ExitUser indicates a user-related error (invalid input, configuration, etc.). ExitUser = 1 // ExitSystem indicates a system-related error (I/O, network, permissions, etc.). ExitSystem = 2 )
Exit codes for CLI applications.
Variables ¶
var ( // ErrMissingName indicates a required name field is missing. ErrMissingName = pkgerrors.New("name is required") // ErrNotFound indicates the requested resource was not found. ErrNotFound = pkgerrors.New("resource not found") // ErrInvalidConfig indicates configuration validation failed. ErrInvalidConfig = pkgerrors.New("invalid configuration") // ErrInvalidToolSyntax indicates a malformed tool permission string. ErrInvalidToolSyntax = pkgerrors.New("invalid tool syntax") // ErrUnknownTool indicates the tool is not in the known tool list. ErrUnknownTool = pkgerrors.New("unknown tool") // ErrPermissionDenied indicates the operation is not permitted. ErrPermissionDenied = pkgerrors.New("permission denied") // ErrNotImplemented indicates the requested feature is not yet implemented. ErrNotImplemented = pkgerrors.New("not implemented") // ErrNotSupported indicates the requested operation is not supported on the platform. ErrNotSupported = pkgerrors.New("not supported") // ErrTimeout indicates the operation timed out. ErrTimeout = pkgerrors.New("operation timed out") // ErrInternal indicates an unexpected internal error. ErrInternal = pkgerrors.New("internal error") // ErrValidation indicates a validation failure. ErrValidation = pkgerrors.New("validation failed") )
Sentinel errors for common failure conditions.
Functions ¶
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true. This is a passthrough to cockroachdb/errors.As.
func Is ¶
Is reports whether any error in err's chain matches target. This is a passthrough to cockroachdb/errors.Is.
func Join ¶
Join returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if every value in errs is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each element.
This is a passthrough to standard library errors.Join.
func New ¶
New creates a new error with the given message. This is a passthrough to cockroachdb/errors.New.
func Newf ¶
Newf creates a new error with a formatted message. This is a passthrough to cockroachdb/errors.Newf.
func WithDetail ¶
WithDetail decorates an error with an arbitrary string detail. This is a passthrough to cockroachdb/errors.WithDetail.
func WithDetailf ¶
WithDetailf decorates an error with a formatted string detail. This is a passthrough to cockroachdb/errors.WithDetailf.
Types ¶
type ExitError ¶
type ExitError struct {
// Err is the underlying error that caused the exit.
Err error
// Code is the exit code to return to the operating system.
Code int
// Suggestion is an optional actionable suggestion for the user.
Suggestion string
}
ExitError wraps an error with an exit code and optional suggestion for CLI applications. It implements the error interface and supports unwrapping via errors.Unwrap.
func NewConfigError ¶
NewConfigError creates an ExitError with ExitUser code and a standard suggestion.
func NewExitError ¶
NewExitError creates an ExitError with the given underlying error and exit code. If err is nil, the returned ExitError will have a nil Err field.
func NewExitErrorWithSuggestion ¶
NewExitErrorWithSuggestion creates an ExitError with a suggestion.
func NewSystemError ¶
NewSystemError creates an ExitError with ExitSystem code and a suggestion.
func NewUserError ¶
NewUserError creates an ExitError with ExitUser code and a suggestion.