exit

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 4 Imported by: 7

Documentation

Overview

Package exit defines exit and error behavior of programs and commands.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Die added in v0.3.4

func Die(str stream.IOStream, err error) error

Die prints a non-nil err to io.Stderr and returns an error of type Error or nil.

When error is non-nil, this function first turns err into type Error. Then if err.Message is not the empty string, it prints it to io.Stderr with wrapping.

If err is nil, it does nothing and returns nil.

Types

type Error

type Error struct {
	// Exit code of the program (if applicable)
	ExitCode
	// Message for this error
	// Messages should pass docfmt.Validate
	Message string
	// contains filtered or unexported fields
}

Error represents any error state by a program. It implements the builtin error interface.

The zero value represents that no error occurred and is ready to use.

func AsError

func AsError(err error) Error

AsError asserts that err is either nil or of type Error and returns it. When err is nil, the zero value of type Error is returned.

If err is not nil and not of type Error, calls panic().

func (Error) DeferWrap added in v0.3.1

func (err Error) DeferWrap(e *error)

DeferWrap ensures that the error pointed to by e is either nil or an Error. If e is neither err.Wrap(*e) is called.

Example
var genericError = Error{ExitCode: ExitGeneric, Message: "generic error"}

// something returns the error it is passed
something := func(in error) (err error) {
	// ensure that err is of type Error!
	// this only updates error which are not yet of type Error.
	defer genericError.DeferWrap(&err)

	return in
}

fmt.Println(something(nil))
fmt.Println(something(errors.New("something went wrong")))
fmt.Println(something(Error{ExitCode: ExitGeneric, Message: "specific error"}))
Output:

<nil>
generic error: something went wrong
specific error

func (Error) Error

func (err Error) Error() string

Error returns the error message belonging to this error.

func (Error) Unwrap added in v0.0.11

func (err Error) Unwrap() error

Unwrap unwraps this error, if any

func (Error) WithMessage

func (err Error) WithMessage(message string) Error

WithMessage returns a copy of this error with the same Code but different Message.

The new message is the message passed as an argument.

func (Error) WithMessageF

func (err Error) WithMessageF(args ...any) Error

WithMessageF returns a copy of this error with the same Code but different Message. The new message is the current message, formatted using a call to SPrintf and the arguments.

func (Error) Wrap deprecated added in v0.0.11

func (err Error) Wrap(inner error) Error

Wrap behaves like WrapError, except that when inner is nil, en empty error is returned. This method exists for legacy reasons.

Deprecated: Use WrapError instead.

func (Error) WrapError added in v0.4.0

func (err Error) WrapError(inner error) error

WrapError creates a new Error with same exit code, wrapping the inner error. When inner is nil, returns nil. This function will return either nil, or an error of type Error.

The message of the new error will contain the Error() result of the inner error.

type ExitCode

type ExitCode uint8

ExitCode determines the exit behavior of a program. These are returned as an exit code to the operating system. See ExitCode.Return().

const (
	// ExitZero indicates that no error occurred.
	// It is the zero value of type ExitCode.
	ExitZero ExitCode = 0

	// ExitGeneric indicates a generic error occurred within this invocation.
	// This typically implies a subcommand-specific behavior wants to return failure to the caller.
	ExitGeneric ExitCode = 1

	// ExitUnknownCommand indicates that the user attempted to call a subcommand that is not defined.
	ExitUnknownCommand ExitCode = 2

	// ExitGeneralArguments indicates that the user attempted to pass invalid general arguments to the program.
	ExitGeneralArguments ExitCode = 3
	// ExitCommandArguments indicates that the user attempted to pass invalid command-specific arguments to a subcommand.
	ExitCommandArguments ExitCode = 4

	// ExitContext indicates an error with the underlying command context.
	ExitContext ExitCode = 254

	// ExitPanic indicates that the go code called panic() inside the execution of the current program.
	// This typically implies a bug inside a program.
	ExitPanic ExitCode = 255
)

func (ExitCode) Return

func (code ExitCode) Return()

Return returns this ExitCode to the operating system by invoking os.Exit().

Jump to

Keyboard shortcuts

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