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
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 ¶
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
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) WithMessage ¶
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 ¶
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.
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 )