errors

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: LGPL-3.0 Imports: 2 Imported by: 0

README

errors package (CLI)

Minimal helpers for CLI-friendly error handling:

  • Silent: never printed
  • Priority + Must Show: clean user messages with full chain preserved for verbose
  • One-liner printing in main

Quick use

// main.go
if pkgerrors.ShouldPrint(err) {
    // User-facing (short): highest priority + must-show
    printer.Errorln(pkgerrors.FormatError(err))
}

Marking errors where they happen

// 1) Silent: do not show this error to user
if userCanceled {
    return pkgerrors.NewSilentf("user canceled")
}

// 2) Priority: user-friendly message, preserved chain
if err := connectDB(); err != nil {
    return pkgerrors.SetPriority("Failed to connect to database", err, pkgerrors.PriorityHigh)
}

// 3) Must show: extra context that must be displayed
if err := api.CheckStatus(); err != nil {
    return pkgerrors.SetMustShow("service unavailable", err)
}

What to call (90% use cases)

// Build final user-facing message from the error chain

  • pkgerrors.FormatError(err error) string

// Silence errors from printing

  • pkgerrors.NewSilent(err error) error
  • pkgerrors.NewSilentf(format string, args ...any) error

// Check if a error is silenced

  • pkgerrors.ShouldPrint(err error) bool

// Priority tagging (highest wins)

  • pkgerrors.SetPriority(msg string, err error, p int) error
  • pkgerrors.SetPriorityf(format string, err error, p int, args ...any) error
  • pkgerrors.NewPriority(err error, p int) error // tag existing err as priority
  • pkgerrors.NewPriorityf(p int, format string, args ...any) error // originate new

// Must-show tagging (always appended after highest priority)

  • pkgerrors.SetMustShow(msg string, err error) error
  • pkgerrors.SetMustShowf(format string, err error, p ...any) error
  • pkgerrors.NewMustShow(err error) error // tag existing err as must-show
  • pkgerrors.NewMustShowf(format string, args ...any) error // originate new

Priority levels:

const (
    PriorityLow      = 1
    PriorityMedium   = 50
    PriorityHigh     = 100
    PriorityCritical = 200
)

Notes

  • ShouldPrint(err) checks the whole chain: if any link is silent, nothing is printed.
  • FormatError(err) walks the chain, picks the highest priority short message, and appends any must-show short messages in order (no duplicates). If no priority exists, it falls back to err.Error() (full chain top-level string).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSilent

func IsSilent(err error) bool

IsSilent checks if an error is marked as silent and shouldn't be printed. errors.As, not eris.As: kong wraps every command error in errors.Join, which eris can't see into.

func JoinFailures added in v0.18.1

func JoinFailures(errs ...error) error

JoinFailures joins errs' real failures. A cancel (Ctrl+C) or silent error isn't one, so it can't hide one; it's returned only if nothing failed.

func NewSilent

func NewSilent(err error) error

NewSilent wraps an error as a silent error that shouldn't be printed.

func ShouldPrint

func ShouldPrint(err error) bool

ShouldPrint returns true if the error should be printed to the user. This is the inverse of IsSilent for more readable code.

Types

type SilentError

type SilentError struct {
	// contains filtered or unexported fields
}

SilentError wraps an error to indicate it should not be printed to the user. This is useful for expected user cancellations, validation failures that have already been handled, etc.

func (*SilentError) Error

func (s *SilentError) Error() string

Error implements the error interface.

func (*SilentError) Unwrap

func (s *SilentError) Unwrap() error

Unwrap implements the unwrapper interface for error chains.

Jump to

Keyboard shortcuts

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