errors

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultWrapWidth = 80

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultHandler

type DefaultHandler struct {
	Output io.Writer
	// contains filtered or unexported fields
}

Handler handles errors providing colored output to a specified writer (defaults to stderr).

func NewDefaultHandler

func NewDefaultHandler(output io.Writer) *DefaultHandler

NewDefaultHandler creates a new DefaultErrorHandler with the specified output writer. If no writer is provided, it defaults to stderr.

func (*DefaultHandler) HandleError

func (h *DefaultHandler) HandleError(err error)

HandleError checks if the given error implements the ErrorWithHelp interface. If it does, it prints the error message and help message in a colored format to the configured output writer. Otherwise, it prints a generic error message.

func (*DefaultHandler) NewGeneralError

func (h *DefaultHandler) NewGeneralError(message, helpMsg string, err error, code int) *GeneralError

func (*DefaultHandler) SetWrap added in v0.8.7

func (h *DefaultHandler) SetWrap(wrap bool)

func (*DefaultHandler) SetWrapWidth added in v0.8.7

func (h *DefaultHandler) SetWrapWidth(width int)

type ErrorWithHelp

type ErrorWithHelp interface {
	error

	// Help returns a help message for the error
	Help() string

	// Unwrap returns the underlying error
	Unwrap() error // Optional: for wrapped errors

	// Code returns the error code
	Code() int // Optional: for error codes
}

ErrorWithHelp interface is used for errors that can provide help

type GeneralError

type GeneralError struct {
	Message string
	HelpMsg string
	CodeVal int
	Err     error
}

func (*GeneralError) Code

func (e *GeneralError) Code() int

func (*GeneralError) Error

func (e *GeneralError) Error() string

func (*GeneralError) Help

func (e *GeneralError) Help() string

func (*GeneralError) Unwrap

func (e *GeneralError) Unwrap() error

type Handler

type Handler interface {
	// HandleError handles the given error.
	HandleError(err error)
	// NewGeneralError creates a new GeneralError with the specified message, help message, error, and code.
	NewGeneralError(message, helpMsg string, err error, code int) *GeneralError
	// SetWrap sets whether to wrap the error message and help message.
	SetWrap(wrap bool)
	// SetWrapWidth sets the width to wrap the error message and help message.
	SetWrapWidth(width int)
}

Handler is an interface for handling errors.

type InvalidArgumentError

type InvalidArgumentError struct {
	Flag     string
	Val      string
	OneOf    []string
	SeeOther string
	Context  string
}

InvalidArgumentError is returned when an argument is invalid

func (*InvalidArgumentError) Code

func (e *InvalidArgumentError) Code() int

func (*InvalidArgumentError) Error

func (e *InvalidArgumentError) Error() string

func (*InvalidArgumentError) Help

func (e *InvalidArgumentError) Help() string

Help returns the error message

func (*InvalidArgumentError) Unwrap

func (e *InvalidArgumentError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳