errfmt

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package errfmt is the central renderer for user-facing CLI errors.

Backend adapters classify wire errors into typed backend.DomainError values carrying a dotted ErrorCode. This package owns the mapping from code → human-readable title + hints, and writes them to the user via Render.

Why a dedicated package: presentation concerns (TTY colour, line shape, hint phrasing) drift over time, and code-level translations live close to the data they describe. Keeping both in one place stops the cmd layer from duplicating ad-hoc message strings.

Callers reach this package via cmdutil.ExplainError (legacy shim that most cmd/* packages still go through) or directly via Render in new code paths.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CatalogueHints added in v1.81.0

func CatalogueHints() map[backend.ErrorCode][]string

CatalogueHints returns the raw (un-expanded) hint templates for every error code in the catalogue. Consumed by the hint↔flag contract test to verify that every flag token in a hint is registered on the root cobra command.

func HintsFor added in v1.19.0

func HintsFor(de *backend.DomainError) []string

HintsFor returns the catalogue's hint strings for de.Code with template placeholders ({{.Host}}, {{.ID}}, {{.Resource}}, {{.Feature}}) expanded against the DomainError. Returns nil when de is nil, when the Code is empty, or when no catalogue entry exists for the code.

MCP and other structured-output surfaces use this to populate the "hints" field of their error envelope so clients see the same remediation steps as CLI users without bundling their own catalogue.

func Render

func Render(ios *iostreams.IOStreams, err error)

Render writes a friendly error explanation to ios.ErrOut.

Resolution order:

  1. nil → no-op (safe to call from generic shims).
  2. DomainError whose Code is present in the catalogue → templated title/cause/hint output.
  3. DomainError whose Code is unset OR not in the catalogue, but whose Kind matches one of backend.ErrConflict / ErrUnsupportedOnHost / ErrPermission / ErrNotFound → Kind-based fallback line.
  4. anything else (including DomainErrors with no Code and no recognised Kind, or non-DomainError values) → raw error string.

Every server- or config-derived field (Message, Host, Resource, ID, Feature) is passed through sanitise before reaching ios.ErrOut to neutralise terminal escape injection (CWE-150 / CWE-117).

Types

This section is empty.

Jump to

Keyboard shortcuts

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