Documentation ¶
Overview ¶
Package exit implements errors represents the status code.
Example ¶
package main import ( "errors" "fmt" "os" "github.com/takumakei/go-exit" ) func main() { // Printing the status code instead of calling os.Exit just for this example. exit.HandleExit = func(code int) { fmt.Printf("status %d", code) } exit.Fexit(os.Stdout, run()) } func run() error { return exit.Error(42, errors.New("Deep Thought said 42")) }
Output: error: Deep Thought said 42 status 42
Index ¶
- Variables
- func Error(code int, err error) error
- func ErrorString(code int, msg string) error
- func Exit(err error)
- func ExitOnError(err error)
- func Fexit(w io.Writer, err error)
- func FexitOnError(w io.Writer, err error)
- func LookupStatusCoder(target error) (code int, ok bool)
- func Status(code int) types.Status
- func StatusCode(err error) int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Format = "error: %v\n"
Format is the format used by Exit to write an error.
var HandleExit = os.Exit
HandleExit is os.Exit. This should not be changed not in case of testing.
var Stderr io.Writer = os.Stderr
Stderr is the writer that Exit writes a message to.
Functions ¶
func Error ¶
Error returns the error that wraps the err with the status code.
returns nil if err == nil and code == 0. returns Status(code) if err == nil. returns types.NewError(code, err), otherwise.
func ErrorString ¶
ErrorString calls Error(code, errors.New(msg)) if msg is not an empty string, otherwise it calls Error(code, nil)
Note that Error(0, nil) returns nil whereas Status(code) returns always non-nil error.
func ExitOnError ¶
func ExitOnError(err error)
ExitOnError calls Exit(err) if err != nil, otherwise do nothing.
func Fexit ¶
Fexit writes the error message of err to w, then calls os.Fexit.
The error message is not written in case of that `err is nil` nor `err is types.Status itself`.
If you do not want to output any error message under any circumstances, consider the following. `os.Exit(StatusCode(err))`
func FexitOnError ¶
FexitOnError calls Fexit(w, err) if err != nil, otherwise do nothing.
func LookupStatusCoder ¶
LookupStatusCoder returns the StatusCode() of types.StatusCoder with ok = true in case of the target contains a types.StatusCoder.
func StatusCode ¶
StatusCode returns the status code for err.
returns 0 if err == nil. returns 1 if err is not a types.StatusCoder. returns StatusCode() of types.StatusCoder, otherwise.
Types ¶
This section is empty.