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 ¶
- Variables
- func IsAuthError(err error) bool
- func IsConnectionError(err error) bool
- func IsPermissionError(err error) bool
- func IsProjectError(err error) bool
- func NewNoProjectLinkedError(opts ...Option) error
- func NewNotAuthenticatedError(opts ...Option) error
- func NewNotInGitRepoError(opts ...Option) error
- func WrapAuthError(err error, opts ...Option) error
- func WrapConnectionError(err error, serverURL string, opts ...Option) error
- func WrapProjectError(err error, opts ...Option) error
- type CLIError
- type DefaultMessenger
- func (m DefaultMessenger) AuthErrorMessage() (string, string)
- func (m DefaultMessenger) ConnectionErrorMessage(serverURL string) (string, string)
- func (m DefaultMessenger) NoProjectLinkedMessage() (string, string)
- func (m DefaultMessenger) NotInGitRepoMessage() (string, string)
- func (m DefaultMessenger) PermissionDeniedMessage() (string, string)
- func (m DefaultMessenger) ProjectNotFoundMessage() (string, string)
- func (m DefaultMessenger) SessionExpiredMessage() (string, string)
- func (m DefaultMessenger) TLSErrorMessage(serverURL string) (string, string)
- func (m DefaultMessenger) TimeoutErrorMessage(serverURL string) (string, string)
- type ErrorMessenger
- type Option
- type WrapConfig
Constants ¶
This section is empty.
Variables ¶
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 ¶
IsAuthError checks if an error is authentication-related.
func IsConnectionError ¶
IsConnectionError checks if an error is connection-related. This includes TLS errors, timeouts, and network connectivity issues.
func IsPermissionError ¶
IsPermissionError checks if an error is permission-related.
func IsProjectError ¶
IsProjectError checks if an error is project-related.
func NewNoProjectLinkedError ¶
NewNoProjectLinkedError creates an error when no project is configured.
func NewNotAuthenticatedError ¶
NewNotAuthenticatedError creates an error for unauthenticated users.
func NewNotInGitRepoError ¶
NewNotInGitRepoError creates an error for commands that require a git repository.
func WrapAuthError ¶
WrapAuthError wraps authentication-related errors with helpful guidance.
func WrapConnectionError ¶
WrapConnectionError wraps connection-related errors with helpful guidance.
func WrapProjectError ¶
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.
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.