clierr

package
v0.269.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clierr centralizes how command errors map to user-facing messages and process exit codes, so main can stay a thin shell and every command surfaces failures consistently.

Index

Constants

View Source
const (
	CodeError      = 1   // generic runtime failure (load/parse errors, I/O, etc.)
	CodeUsage      = 2   // bad invocation (unknown flag, wrong arg count)
	CodeTestFailed = 3   // tests ran and some failed: `model test` (or coverage below --coverage-min), or `assertions test`
	CodeNetwork    = 4   // could not reach the OpenFGA server
	CodeCanceled   = 130 // interrupted (Ctrl-C); 128 + SIGINT
)

Process exit codes. Scripts and CI can branch on these instead of parsing stderr text.

Variables

This section is empty.

Functions

func Code

func Code(err error) int

Code resolves the exit code for err: an explicit Coded wins, then an interruption maps to CodeCanceled, a bad invocation to CodeUsage, a network failure to CodeNetwork, and anything else to CodeError.

func Friendly

func Friendly(err error) string

Friendly renders err for a human: connection failures get an actionable hint (with the original error kept for detail); everything else is returned as-is.

func HasKind added in v0.268.0

func HasKind(err error) bool

HasKind reports whether err carries an explicit Kind set with WithKind.

func IsBrokenPipe

func IsBrokenPipe(err error) bool

IsBrokenPipe reports that the downstream stdout consumer closed its end of a pipe. This is normal shell control flow (for example `ofga ... | head`), not an OpenFGA network failure.

func IsConnErr

func IsConnErr(err error) bool

IsConnErr reports whether err looks like a network-level failure (refused connection, DNS lookup failure, timeout) rather than a normal API error. It checks the idiomatic net.Error interface first, then falls back to matching well-known substrings for errors that don't implement it.

func IsIgnorableBrokenPipe

func IsIgnorableBrokenPipe(err error) bool

IsIgnorableBrokenPipe reports a plain stdout EPIPE that may be treated as a successful short read by a downstream consumer. Explicitly coded failures still win even if an output write also encountered EPIPE.

func IsSilent added in v0.266.0

func IsSilent(err error) bool

IsSilent reports whether err is a Silent error whose message was already surfaced to the user and should not be printed again.

func IsUsageErr

func IsUsageErr(err error) bool

IsUsageErr reports whether err is one of cobra's or pflag's flag/argument validation failures (missing required flag, unknown flag/command, wrong arg count). These are bad invocations, not runtime failures, so they map to CodeUsage and a "--help" hint.

pflag returns a distinct type for each flag-parsing failure, so those are matched structurally with errors.As. Cobra's own arg-count/required-flag/ unknown-command errors have no dedicated type — they're plain strings in a fixed format (args.go, command.go) — so matching is anchored to that exact prefix with strings.HasPrefix. An unanchored substring match would also catch unrelated errors that merely mention the same words, e.g. an OS EINVAL surfacing as "open <path>: invalid argument", or a wrapped server message containing "requires"/"accepts" mid-sentence.

func Silent added in v0.266.0

func Silent(code int, summary string) error

Silent tags an exit code for a failure the command has already reported to the user (e.g. a model-test "N/Total test(s) failed" summary line). main honors the exit code but prints nothing further, so the summary is not duplicated. summary is that one-line human summary; it feeds the machine error envelope. Use WithCode instead when the error carries a message the user still needs to see.

func SilentCause added in v0.268.0

func SilentCause(code int, summary string, cause error) error

SilentCause is Silent for a failure caused by cause, which stays in the chain so the machine error envelope can report its status and API code.

func SilentSummary added in v0.268.0

func SilentSummary(err error) string

SilentSummary returns the summary a Silent error carries, or "" if err is not Silent.

func WithCode

func WithCode(code int, err error) error

WithCode tags err with a specific exit code. Returns nil for a nil error.

func WithDetails added in v0.268.0

func WithDetails(err error, details ...map[string]any) error

WithDetails appends structured detail objects to err. Returns nil for a nil error.

func WithHint added in v0.268.0

func WithHint(hint string, err error) error

WithHint attaches a remediation hint to err. Returns nil for a nil error; the outermost WithHint wins.

func WithKind added in v0.268.0

func WithKind(k Kind, err error) error

WithKind tags err with an explicit Kind. Returns nil for a nil error; the outermost WithKind wins.

func WithPartialResult added in v0.267.0

func WithPartialResult(err error) error

WithPartialResult tags err as carrying partial-commit detail. Returns nil for a nil error.

Types

type Coded

type Coded struct {
	C   int
	Err error
}

Coded wraps an error with an explicit process exit code.

func (*Coded) Error

func (e *Coded) Error() string

func (*Coded) Unwrap

func (e *Coded) Unwrap() error

type Envelope added in v0.268.0

type Envelope struct {
	Error EnvelopeBody `json:"error"`
}

Envelope is the JSON document written to stderr for a failed command under machine output.

func NewEnvelope added in v0.268.0

func NewEnvelope(err error) Envelope

NewEnvelope builds the machine-readable form of err.

func (Envelope) WriteLine added in v0.268.0

func (e Envelope) WriteLine(w io.Writer) error

WriteLine writes the envelope as one compact JSON line.

type EnvelopeBody added in v0.268.0

type EnvelopeBody struct {
	Code    Kind             `json:"code"`
	Message string           `json:"message"`
	Status  int              `json:"status,omitempty"`
	APICode string           `json:"api_code,omitempty"`
	Hint    string           `json:"hint,omitempty"`
	Details []map[string]any `json:"details"`
}

EnvelopeBody is the payload of an Envelope.

type Kind added in v0.268.0

type Kind string

Kind is a stable, machine-readable error category. Agents branch on it instead of parsing messages.

const (
	KindUsage                Kind = "usage"
	KindConfirmationRequired Kind = "confirmation_required"
	KindValidation           Kind = "validation"
	KindAlreadyExists        Kind = "already_exists"
	KindUnauthenticated      Kind = "unauthenticated"
	KindForbidden            Kind = "forbidden"
	KindNotFound             Kind = "not_found"
	KindRateLimited          Kind = "rate_limited"
	KindServer               Kind = "server"
	KindNetwork              Kind = "network"
	KindTestFailed           Kind = "test_failed"
	KindPartialFailure       Kind = "partial_failure"
	KindCanceled             Kind = "canceled"
	KindError                Kind = "error"
)

Error kinds carried in the JSON error envelope's "code" field.

func Classify added in v0.268.0

func Classify(err error) Kind

Classify maps err to its stable Kind.

func Kinds added in v0.268.0

func Kinds() []Kind

Kinds lists every Kind, in the order the error schema publishes them.

type PartialResult added in v0.267.0

type PartialResult struct {
	Err error
}

PartialResult marks an error from an interrupted bulk operation (e.g. a tuple write that stopped partway through a batch) whose message already carries "N of M committed" detail. A Ctrl-C/SIGTERM cancellation handler can errors.As for this to still report what landed, instead of only "canceled".

func (*PartialResult) Error added in v0.267.0

func (e *PartialResult) Error() string

func (*PartialResult) Unwrap added in v0.267.0

func (e *PartialResult) Unwrap() error

Jump to

Keyboard shortcuts

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