Documentation
¶
Overview ¶
Package errslice is for working with multiple errors, especially for returning them to callers that may be expecting only one.
See the AppendCall example for typical usage.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append concatenates errors and Slices, dropping nils. It flattens Slices, but not recursively. If not passed at least one non-nil argument, it will return nil, and if passed only one non-nil argument, it will return that one, unchanged.
func AppendCall ¶
AppendCall calls appends the result of calling f to errp. AppendCall is useful when deferring calls that return an error.
Example ¶
package main
import (
"errors"
"fmt"
"io"
"os"
"sethwklein.net/errslice"
)
type angryWriteCloser struct{}
func (_ angryWriteCloser) Write(b []byte) (int, error) {
return 0, errors.New("no writing!")
}
func (_ angryWriteCloser) Close() error {
return errors.New("no closing!")
}
// BE CAREFUL! The return value must be named. Else the error returned from the
// deferred call will be silently discarded with no compiler error.
func exampleWrite(w io.WriteCloser) (err error) {
defer errslice.AppendCall(&err, w.Close)
for c := byte('a'); c <= 'z'; c++ {
_, err = w.Write([]byte{c})
if err != nil {
return err
}
}
return nil
}
func main() {
err := exampleWrite(angryWriteCloser{})
for _, e := range errslice.From(err) {
// In real code, use os.Stderr, but that's broken in examples.
fmt.Fprintln(os.Stdout, "Error:", e)
}
}
Output: Error: no writing! Error: no closing!
Types ¶
type Slice ¶
type Slice []error
Slice is a []error that implements error.
func From ¶
From wraps err in a Slice unless it already is a Slice or []error by another name, in which case it returns it unaltered or uses reflection to convert it to a Slice, respectively. If err is nil, From returns nil.
From is useful for iterating over errors, such as when printing them. See AppendCall's example for typical usage.
Notes ¶
Bugs ¶
should I expose fromByReflection? I used it for higher performance in Append.