errorhandling

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 7 Imported by: 0

README

Error Handling

Centralized error reporting and formatting for consistent CLI behavior.

Key Features:

  • Structured error logging
  • Clean stack trace formatting (in debug mode)
  • Contextual help integration
  • Consistent exit code management

For detailed documentation and best practices, see the Error Handling Component Documentation.

Documentation

Index

Constants

View Source
const (
	LevelFatal    = "fatal"
	LevelError    = "error"
	LevelWarn     = "warn"
	KeyStacktrace = "stacktrace"
	KeyHelp       = "help"
	KeyHints      = "hints"
	KeyDetails    = "details"
)

Variables

View Source
var (
	ErrNotImplemented = errors.New("command not yet implemented")
	ErrRunSubCommand  = errors.New("subcommand required")
)

Functions

func NewAssertionFailure

func NewAssertionFailure(format string, args ...any) error

NewAssertionFailure creates an error denoting a programming bug.

func NewErrNotImplemented

func NewErrNotImplemented(issueURL string) error

NewErrNotImplemented creates an unimplemented error with an optional issue tracker link.

func WithUserHint

func WithUserHint(err error, hint string) error

WithUserHint attaches a user-facing recovery suggestion to an error.

func WithUserHintf

func WithUserHintf(err error, format string, args ...any) error

WithUserHintf attaches a formatted user-facing recovery suggestion.

func WrapWithHint

func WrapWithHint(err error, msg string, hint string) error

WrapWithHint wraps an error with a message and attaches a user-facing hint.

Types

type ErrorHandler

type ErrorHandler interface {
	Check(err error, prefix string, level string, cmd ...*cobra.Command)
	Fatal(err error, prefixes ...string)
	Error(err error, prefixes ...string)
	Warn(err error, prefixes ...string)
	SetUsage(usage func() error)
}

func New

func New(logger *log.Logger, help HelpConfig, opts ...Option) ErrorHandler

type ExitFunc

type ExitFunc func(code int)

type HelpConfig

type HelpConfig interface {
	SupportMessage() string
}

HelpConfig is the interface for providing contextual support information when errors occur. Implementations return a human-readable message directing users to a support channel. Returning an empty string suppresses the help output.

type Option

type Option func(*StandardErrorHandler)

func WithExitFunc

func WithExitFunc(exit ExitFunc) Option

WithExitFunc allows injection of a custom exit handler (e.g. for testing).

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter allows injection of a custom output writer.

type SlackHelp

type SlackHelp struct {
	Team    string `json:"team" yaml:"team"`
	Channel string `json:"channel" yaml:"channel"`
}

SlackHelp provides Slack channel contact details as a support message.

func (SlackHelp) SupportMessage

func (s SlackHelp) SupportMessage() string

type StandardErrorHandler

type StandardErrorHandler struct {
	Logger *log.Logger
	Help   HelpConfig
	Exit   ExitFunc
	Writer io.Writer
	Usage  func() error
}

func (*StandardErrorHandler) Check

func (h *StandardErrorHandler) Check(err error, prefix string, level string, cmd ...*cobra.Command)

func (*StandardErrorHandler) Error

func (h *StandardErrorHandler) Error(err error, prefixes ...string)

func (*StandardErrorHandler) Fatal

func (h *StandardErrorHandler) Fatal(err error, prefixes ...string)

func (*StandardErrorHandler) SetUsage

func (h *StandardErrorHandler) SetUsage(usage func() error)

func (*StandardErrorHandler) Warn

func (h *StandardErrorHandler) Warn(err error, prefixes ...string)

type TeamsHelp

type TeamsHelp struct {
	Team    string `json:"team" yaml:"team"`
	Channel string `json:"channel" yaml:"channel"`
}

TeamsHelp provides Microsoft Teams channel contact details as a support message.

func (TeamsHelp) SupportMessage

func (t TeamsHelp) SupportMessage() string

Directories

Path Synopsis
vcs

Jump to

Keyboard shortcuts

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