errors

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 3 Imported by: 0

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

View Source
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

View Source
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

func As(err error, target interface{}) bool

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 Cause

func Cause(err error) error

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target. This is a passthrough to cockroachdb/errors.Is.

func Join

func Join(errs ...error) error

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

func New(msg string) error

New creates a new error with the given message. This is a passthrough to cockroachdb/errors.New.

func Newf

func Newf(format string, args ...interface{}) error

Newf creates a new error with a formatted message. This is a passthrough to cockroachdb/errors.Newf.

func WithDetail

func WithDetail(err error, msg string) error

WithDetail decorates an error with an arbitrary string detail. This is a passthrough to cockroachdb/errors.WithDetail.

func WithDetailf

func WithDetailf(err error, format string, args ...interface{}) error

WithDetailf decorates an error with a formatted string detail. This is a passthrough to cockroachdb/errors.WithDetailf.

func Wrap

func Wrap(err error, msg string) error

Wrap wraps an error with a message. This is a passthrough to cockroachdb/errors.Wrap.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf wraps an error with a formatted message. This is a passthrough to cockroachdb/errors.Wrapf.

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

func NewConfigError(err error) *ExitError

NewConfigError creates an ExitError with ExitUser code and a standard suggestion.

func NewExitError

func NewExitError(err error, code int) *ExitError

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

func NewExitErrorWithSuggestion(err error, code int, suggestion string) *ExitError

NewExitErrorWithSuggestion creates an ExitError with a suggestion.

func NewSystemError

func NewSystemError(err error, suggestion string) *ExitError

NewSystemError creates an ExitError with ExitSystem code and a suggestion.

func NewUserError

func NewUserError(err error, suggestion string) *ExitError

NewUserError creates an ExitError with ExitUser code and a suggestion.

func (*ExitError) Error

func (e *ExitError) Error() string

Error returns the error message from the underlying error. If the underlying error is nil, it returns a generic message with the exit code.

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is and errors.As to examine the error chain.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL