exit

package module
v0.0.0-...-47cd478 Latest Latest
Warning

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

Go to latest
Published: May 15, 2021 License: Apache-2.0 Imports: 5 Imported by: 20

README

Package exit implements errors represents the status code.

GoDoc

Purpose

  1. eliminating a boiler plate.
  2. specifying an status code.

1 eliminating a boiler plate.

The package exit eliminates a boiler plate for main() like below.

without using the package exit
func main() {
  if err := run(); err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    os.Exit(1)
  }
}
with using the package exit
func main() {
  exit.ExitOnError(run())
}

2 specifying an status code.

the status code WITHOUT the error message

Using exit.Status, exit.Exit does not write any error messages, calls just os.Exit(42).

exit.Exit(exit.Status(42))
the status code WITH the error message

Using exit.Error, exit.Exit writes the error message of err, then calls os.Exit(42).

exit.Exit(exit.Error(42, errors.New("Deep Thought said")))

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

Examples

Constants

This section is empty.

Variables

View Source
var Format = "error: %v\n"

Format is the format used by Exit to write an error.

View Source
var HandleExit = os.Exit

HandleExit is os.Exit. This should not be changed not in case of testing.

View Source
var Stderr io.Writer = os.Stderr

Stderr is the writer that Exit writes a message to.

Functions

func Error

func Error(code int, err error) 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

func ErrorString(code int, msg string) error

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 Exit

func Exit(err error)

Exit calls Fexit(Stderr, err).

func ExitOnError

func ExitOnError(err error)

ExitOnError calls Exit(err) if err != nil, otherwise do nothing.

func Fexit

func Fexit(w io.Writer, err error)

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

func FexitOnError(w io.Writer, err error)

FexitOnError calls Fexit(w, err) if err != nil, otherwise do nothing.

func LookupStatusCoder

func LookupStatusCoder(target error) (code int, ok bool)

LookupStatusCoder returns the StatusCode() of types.StatusCoder with ok = true in case of the target contains a types.StatusCoder.

func Status

func Status(code int) types.Status

Status returns the error that represents the status code.

func StatusCode

func StatusCode(err error) int

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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