Documentation ¶
Overview ¶
Package errors contains some simple error helpers
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Defer ¶
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
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 ¶
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.
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