kerrors

package
v0.0.0-...-640cb6d Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package kerrors defines kraai's error types. KError wraps a cause built with cockroachdb/errors, so a stack is always captured, and tags it with a Code that maps to a fixed process exit code. Only cmd/kraai inspects the exit code, decides whether to print the stack, and exits.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExitCode

func ExitCode(err error) int

ExitCode maps err to a process exit code: 0 for nil, the *KError's own code when one is anywhere in the chain, and CodeUnexpected's otherwise.

func Wrap

func Wrap(cause error, code Code, format string, args ...any) error

Wrap wraps cause as a *KError with a formatted message and code, for a failure that originated outside kraai. A nil cause returns nil.

Wrap returns error, not *KError, so that nil is a true nil interface rather than a typed nil whose methods panic. The other constructors never return nil and keep *KError, so callers reach Code without an assertion.

Types

type Code

type Code int

Code identifies a kraai error's failure category. Each Code has a fixed ExitCode; the table is small and CI-branchable on purpose.

const (
	// CodeUnexpected is the fallback bucket, including any error that is
	// not a *KError at all.
	CodeUnexpected Code = 1
	// CodeValidation marks a manifest, input or config validation failure.
	CodeValidation Code = 2
	// CodeLockHeld marks an environment lock another operation holds.
	CodeLockHeld Code = 3
	// CodeConfirmationRequired marks a protected operation whose required
	// confirmation was missing or did not match.
	CodeConfirmationRequired Code = 4
)

Values are explicit, not iota-derived: the exit-code table is a public CI contract, so inserting a Code must never shift a downstream exit code.

func (Code) ExitCode

func (c Code) ExitCode() int

ExitCode returns the process exit code for c. The two are the same number today; the named conversion is so no call site assumes it.

func (Code) String

func (c Code) String() string

String implements fmt.Stringer for readable error and test output.

type KError

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

KError is kraai's base error type. Build one with New, Wrap or a typed constructor, never a struct literal, since the cause must be constructed through cockroachdb/errors to capture a stack.

func ConfirmationRequired

func ConfirmationRequired(format string, args ...any) *KError

ConfirmationRequired creates a *KError in the CodeConfirmationRequired bucket.

func LockHeld

func LockHeld(format string, args ...any) *KError

LockHeld creates a *KError in the CodeLockHeld bucket.

func New

func New(format string, args ...any) *KError

New creates a *KError in the CodeUnexpected bucket with a formatted message. Use a typed constructor when the failure fits a specific bucket.

func Validation

func Validation(format string, args ...any) *KError

Validation creates a *KError in the CodeValidation bucket.

func (*KError) Code

func (e *KError) Code() Code

Code returns e's failure category.

func (*KError) Error

func (e *KError) Error() string

Error implements error, returning the message chain and never a stack.

func (*KError) ExitCode

func (e *KError) ExitCode() int

ExitCode returns the process exit code for e.

func (*KError) Format

func (e *KError) Format(s fmt.State, verb rune)

Format implements fmt.Formatter by delegating to the cause: %v, %s and %q print the message chain, %+v adds the captured stack.

func (*KError) Unwrap

func (e *KError) Unwrap() error

Unwrap exposes the cause so errors.Is and errors.As traverse into it.

Jump to

Keyboard shortcuts

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