errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides CLI error patterns with user-friendly messaging.

Core types:

  • CLIError: Wraps errors with message, suggestion, and details
  • ErrorMessenger: Interface for customizing error messages

Sentinel errors for common scenarios:

  • ErrNotAuthenticated: User needs to log in
  • ErrSessionExpired: Auth token has expired
  • ErrNotInGitRepo: Command requires a git repository
  • ErrNoProjectLinked: No project is configured
  • ErrConnectionFailed: Server is unreachable
  • ErrPermissionDenied: Insufficient permissions

Example usage:

// Wrap an auth error with default messages
if err := doAuthThing(); err != nil {
    return errors.WrapAuthError(err)
}

// Wrap with custom messages
type MyMessenger struct{}
func (m MyMessenger) AuthErrorMessage() (string, string) {
    return "Please log in.", "Run 'myapp login' to authenticate."
}

wrapped := errors.WrapAuthError(err, errors.WithMessenger(MyMessenger{}))

// Check error types
if errors.IsAuthError(err) {
    // Handle auth-related error
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAuthenticated indicates the user needs to log in.
	ErrNotAuthenticated = errors.New("not authenticated")

	// ErrNotInGitRepo indicates the command requires a git repository.
	ErrNotInGitRepo = errors.New("not in a git repository")

	// ErrNoProjectLinked indicates no project is configured.
	ErrNoProjectLinked = errors.New("no project linked")

	// ErrConnectionFailed indicates the server is unreachable.
	ErrConnectionFailed = errors.New("connection failed")

	// ErrPermissionDenied indicates insufficient permissions.
	ErrPermissionDenied = errors.New("permission denied")

	// ErrSessionExpired indicates the auth token has expired.
	ErrSessionExpired = errors.New("session expired")
)

Common CLI errors with actionable guidance.

Functions

func IsAuthError

func IsAuthError(err error) bool

IsAuthError checks if an error is authentication-related.

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError checks if an error is connection-related. This includes TLS errors, timeouts, and network connectivity issues.

func IsPermissionError

func IsPermissionError(err error) bool

IsPermissionError checks if an error is permission-related.

func IsProjectError

func IsProjectError(err error) bool

IsProjectError checks if an error is project-related.

func NewNoProjectLinkedError

func NewNoProjectLinkedError(opts ...Option) error

NewNoProjectLinkedError creates an error when no project is configured.

func NewNotAuthenticatedError

func NewNotAuthenticatedError(opts ...Option) error

NewNotAuthenticatedError creates an error for unauthenticated users.

func NewNotInGitRepoError

func NewNotInGitRepoError(opts ...Option) error

NewNotInGitRepoError creates an error for commands that require a git repository.

func WrapAuthError

func WrapAuthError(err error, opts ...Option) error

WrapAuthError wraps authentication-related errors with helpful guidance.

func WrapConnectionError

func WrapConnectionError(err error, serverURL string, opts ...Option) error

WrapConnectionError wraps connection-related errors with helpful guidance.

func WrapProjectError

func WrapProjectError(err error, opts ...Option) error

WrapProjectError wraps project-related errors with helpful guidance.

Types

type CLIError

type CLIError struct {
	// Err is the underlying error
	Err error

	// Message is a user-friendly description of what went wrong
	Message string

	// Suggestion is an actionable hint for the user
	Suggestion string

	// Details provides additional context (optional)
	Details string
}

CLIError wraps an error with user-friendly context and suggestions.

func (*CLIError) Error

func (e *CLIError) Error() string

func (*CLIError) Unwrap

func (e *CLIError) Unwrap() error

type DefaultMessenger

type DefaultMessenger struct{}

DefaultMessenger provides default error messages.

func (DefaultMessenger) AuthErrorMessage

func (m DefaultMessenger) AuthErrorMessage() (string, string)

func (DefaultMessenger) ConnectionErrorMessage

func (m DefaultMessenger) ConnectionErrorMessage(serverURL string) (string, string)

func (DefaultMessenger) NoProjectLinkedMessage

func (m DefaultMessenger) NoProjectLinkedMessage() (string, string)

func (DefaultMessenger) NotInGitRepoMessage

func (m DefaultMessenger) NotInGitRepoMessage() (string, string)

func (DefaultMessenger) PermissionDeniedMessage

func (m DefaultMessenger) PermissionDeniedMessage() (string, string)

func (DefaultMessenger) ProjectNotFoundMessage

func (m DefaultMessenger) ProjectNotFoundMessage() (string, string)

func (DefaultMessenger) SessionExpiredMessage

func (m DefaultMessenger) SessionExpiredMessage() (string, string)

func (DefaultMessenger) TLSErrorMessage

func (m DefaultMessenger) TLSErrorMessage(serverURL string) (string, string)

func (DefaultMessenger) TimeoutErrorMessage

func (m DefaultMessenger) TimeoutErrorMessage(serverURL string) (string, string)

type ErrorMessenger

type ErrorMessenger interface {
	// AuthErrorMessage returns the message and suggestion for unauthenticated errors.
	AuthErrorMessage() (message, suggestion string)

	// SessionExpiredMessage returns the message and suggestion for expired sessions.
	SessionExpiredMessage() (message, suggestion string)

	// PermissionDeniedMessage returns the message and suggestion for permission errors.
	PermissionDeniedMessage() (message, suggestion string)

	// ConnectionErrorMessage returns the message and suggestion for connection errors.
	// The serverURL parameter is the URL that failed to connect.
	ConnectionErrorMessage(serverURL string) (message, suggestion string)

	// TLSErrorMessage returns the message and suggestion for TLS/certificate errors.
	TLSErrorMessage(serverURL string) (message, suggestion string)

	// TimeoutErrorMessage returns the message and suggestion for timeout errors.
	TimeoutErrorMessage(serverURL string) (message, suggestion string)

	// NotInGitRepoMessage returns the message and suggestion for git repo errors.
	NotInGitRepoMessage() (message, suggestion string)

	// NoProjectLinkedMessage returns the message and suggestion for project errors.
	NoProjectLinkedMessage() (message, suggestion string)

	// ProjectNotFoundMessage returns the message and suggestion for missing projects.
	ProjectNotFoundMessage() (message, suggestion string)
}

ErrorMessenger provides customizable error messages. Implement this interface to customize suggestions for your CLI.

type Option

type Option func(*WrapConfig)

Option configures WrapConfig.

func WithMessenger

func WithMessenger(m ErrorMessenger) Option

WithMessenger sets a custom error messenger.

type WrapConfig

type WrapConfig struct {
	Messenger ErrorMessenger
}

WrapConfig configures error wrapping behavior.

Jump to

Keyboard shortcuts

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