errors

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2019 License: MIT Imports: 5 Imported by: 4

README

Errors GoDoc Go Report Card

Some Go error helpers

Documentation

Overview

Package errors contains some simple error helpers

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Defer

func Defer(err *error, f func() error)

Defer is for use when defering a function call that can return an error. If the referenced error is nil but the callback returns a non-nil error, it sets the reference to the value of the returned error.

Example

Calling Close() on an io.WriteCloser can return an important error encountered while flushing to disk. Don't risk missing them by using a plain defer w.Close(). Use errors.Defer to capture the return value.

package main

import (
	"fmt"

	"github.com/carlmjohnson/errors"
)

type closer struct{}

func (c closer) Close() error {
	return fmt.Errorf("<had problem closing!>")
}

func openThingie() (c closer, err error) { return }

// Calling Close() on an io.WriteCloser can return an important error
// encountered while flushing to disk. Don't risk missing them by
// using a plain defer w.Close(). Use errors.Defer to capture the return value.
func main() {
	// If you just defer a close call, you can miss an error
	return1 := func() error {
		thing, err := openThingie()
		if err != nil {
			return err
		}
		defer thing.Close() // oh no, this returned an error!
		// do stuff...
		return err
	}()
	fmt.Println(return1) // == <nil>

	// Use errors.Defer and a named return to capture the error
	return2 := func() (err error) {
		thing, err := openThingie()
		if err != nil {
			return err
		}
		defer errors.Defer(&err, thing.Close)
		// do stuff...
		return err
	}()
	fmt.Println(return2) // == <had problem closing!>

}
Output:

<nil>
<had problem closing!>

func Execute added in v0.0.6

func Execute(f func([]string) error, args []string) (exitCode int)

Execute is intended to be used as the top level function call of a CLI. It passes args to the callback. If args is nil, os.Args[1:] is substituted. If the callback returns a nil error, Execute does nothing and returns 0. Otherwise, Execute prints the error to stderr and returns non-zero. Specific error codes can be specified by implementing the ExitCoder optional interface.

func Merge

func Merge(errs ...error) error

Merge is a convenience method for making a Slice of errors and calling the Merge method.

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/errors"
)

func main() {
	// A function that sometimes returns an error
	called := 0
	someFunc := func() error {
		called++
		if called%2 == 0 {
			return fmt.Errorf("even error: %d!", called)
		}
		return nil
	}

	// We do a series of operations that might return an error.
	err := someFunc()

	// This time, it didn't return an error.
	fmt.Printf("%+v\n", err)

	// After each operation, we merge it into our existing error variable
	// then do the next operation.
	err = errors.Merge(err, someFunc())
	err = errors.Merge(err, someFunc())
	err = errors.Merge(err, someFunc())

	// Finally, we return the result
	fmt.Printf("%+v", err)
}
Output:

<nil>
2 errors:
	error 1: even error: 2!
	error 2: even error: 4!

Types

type ExitCoder added in v0.0.6

type ExitCoder interface {
	ExitCode() int
}

ExitCoder is an optional interface that errors can implement to change what exit code they cause Execute to return.

type Multierr added in v0.0.7

type Multierr struct {
	// contains filtered or unexported fields
}

Multierr wraps multiple errors.

func (Multierr) Error added in v0.0.7

func (m Multierr) Error() string

Error implements the error interface.

func (Multierr) Format added in v0.0.7

func (m Multierr) Format(state fmt.State, verb rune)

Format implements fmt.Formatter.

func (Multierr) Slice added in v0.0.7

func (m Multierr) Slice() Slice

Slice returns the underlying slice of errors.

func (Multierr) Strings added in v0.0.7

func (m Multierr) Strings() []string

Strings returns the strings from the underlying errors.

type Slice

type Slice []error

Slice is a slice of errors that implements the error interface itself.

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/errors"
)

func main() {
	// A function that sometimes returns an error
	called := 0
	someFunc := func() error {
		called++
		if called%2 == 0 {
			return fmt.Errorf("even error!")
		}
		return nil
	}

	// The empty value can be used
	var errs errors.Slice

	// Do something that returns an error sometimes
	err := someFunc()
	errs.Push(err)

	// Now merging them to produces <nil> error
	fmt.Println(errs.Merge())

	// But if we add non-nil errors...
	err = someFunc()
	errs.Push(err)
	errs.Push(err)

	// Merge returns non-nil
	fmt.Println(errs.Merge())
}
Output:

<nil>
2 errors: even error!; even error!
Example (ExtendedFormat)
package main

import (
	"fmt"

	"github.com/carlmjohnson/errors"
)

func main() {
	var errs errors.Slice

	// Collect several errors
	err := fmt.Errorf("error 1")
	errs.Push(err)

	err = fmt.Errorf("error 2")
	errs.Push(err)

	// ...and a nil error
	err = nil
	errs.Push(err)

	// ...then a real error again
	err = fmt.Errorf("error 3")
	errs.Push(err)

	// Now merge and output them in extended format
	fmt.Printf("%+v", errs.Merge())

}
Output:

3 errors:
	error 1: error 1
	error 2: error 2
	error 3: error 3

func (*Slice) Merge

func (s *Slice) Merge() error

Merge first removes any nil errors from the Slice. If the resulting length of the Slice is zero, it returns nil. If there is only one error, it returns that error as is. If there are multiple errors, it returns a Multierr containing all the errors.

func (*Slice) Push

func (s *Slice) Push(err error)

Push extends a Slice with an error if the error is non-nil.

If a Slice is passed to Push, the result is flattened.

Jump to

Keyboard shortcuts

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