Help us shape the future of Go! Take the 2026 Go Developer Survey

errors

package
v0.7.1 Latest Latest
Warning

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

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

Documentation

Overview

Package errors is the exit-code contract shared by gotools packages: an error that carries the process exit code and the message for stderr.

Only the top-level entry point turns an Error into an exit; every other layer returns it. The codes are documented for users in the help text and docs/USAGE.md.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Table = []Doc{
	{OK, "Success", "Everything worked", "Proceed"},
	{Generic, "Generic failure", "Something went wrong (catch-all)", "Fail the build"},
	{Usage, "Usage error", "Bad flags, wrong args, invalid input", "Fail the build (no retry)"},
	{Network, "Network error", "Proxy unreachable, DNS failure, timeout", "Retry with backoff"},
	{Lock, "Lock contention", "Another gotools process holds the lock", "Wait and retry"},
	{ToolNotFound, "Tool not found", "Requested tool isn't installed", "Run `gotools sync` and retry"},
	{Offline, "Offline required", "Network needed but `--offline` set", "Run `gotools sync` locally"},
	{Policy, "Policy violation", "Tool banned, version not pinned, vuln found", "Block merge"},
	{Environment, "Environment error", "Go missing/too old, bad manifest schema", "Fix environment"},
}

Table is the user-facing exit-code table, in code order. The help text is built from it, so it cannot drift from the codes.

Functions

This section is empty.

Types

type Code

type Code int

Code is a process exit code.

const (
	OK           Code = 0
	Generic      Code = 1
	Usage        Code = 2
	Network      Code = 3
	Lock         Code = 4
	ToolNotFound Code = 5
	Offline      Code = 6
	Policy       Code = 7
	Environment  Code = 8
)

func CodeOf

func CodeOf(err error) Code

CodeOf returns the exit code for err: OK for nil, the carried code for an *Error anywhere in the chain, Generic otherwise.

Example

Callers pick the exit code with CodeOf wherever the error surfaced.

package main

import (
	"fmt"

	ierrors "github.com/piusalfred/gotools/internal/errors"
)

func main() {
	err := fmt.Errorf("install: %w", ierrors.New(ierrors.Offline, "offline mode forbids network access"))

	fmt.Println(ierrors.CodeOf(err))
	fmt.Println(err)

}
Output:
6
install: offline mode forbids network access

type Doc

type Doc struct {
	Code    Code
	Name    string
	Meaning string
	Action  string // what a CI job should do
}

Doc documents one exit code for users.

type Error

type Error struct {
	Code Code
	Msg  string
	Err  error
}

Error carries an exit code and the message for stderr. Err, when set, is the underlying cause and is reachable through errors.Is and errors.As.

func New

func New(code Code, format string, args ...any) *Error

New returns an *Error with a formatted message.

func Wrap

func Wrap(code Code, msg string, err error) *Error

Wrap returns an *Error that carries err as its cause. msg may be empty, in which case the message is err's.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Jump to

Keyboard shortcuts

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