Documentation ¶
Index ¶
- Variables
- func Append(left error, right error) error
- func Combine(errors ...error) error
- func Errorf(format string, args ...interface{}) error
- func New(message string) error
- func WithStack(err error) error
- func Wrap(err error, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type BizError
- type ErrGroup
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInternal - ErrInternal = NewBizError(-1, "internal service error") ErrUnauthorized = NewBizError(-2, "unauthorized") // ErrForbidden - ErrForbidden = NewBizError(-3, "forbidden") )
Functions ¶
func Append ¶ added in v0.1.2
Append - appends the given errors together. Either value may be nil.
This function is a specialization of Combine for the common case where there are only two errors.
func Combine ¶ added in v0.1.2
Combine - Combine combines the passed errors into a single error. If zero arguments were passed or if all items are nil, a nil error is returned.
func Errorf ¶ added in v0.1.4
Errorf - formats according to a format specifier and returns the string as a value that satisfies error. Errorf also records the stack trace at the point it was called. In your application code, use nerrors.New or nerrros.Errorf to return errors.
func New ¶ added in v0.1.4
New - returns an error with the supplied message. New also records the stack trace at the point it was called. In your application code, use nerrors.New or nerrros.Errorf to return errors.
func WithStack ¶ added in v0.1.4
WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil. If collaborating with other libraries, consider using nerrors.WithStack nerrors.Wrap or errors.Wrapf to store stack information. The same applies when working with standard libraries.
func Wrap ¶ added in v0.1.4
Wrap - returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil. If collaborating with other libraries, consider using nerrors.WithStack nerrors.Wrap or errors.Wrapf to store stack information. The same applies when working with standard libraries.
func Wrapf ¶ added in v0.1.4
Wrapf - returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. If err is nil, Wrapf returns nil. If collaborating with other libraries, consider using nerrors.WithStack nerrors.Wrap or errors.Wrapf to store stack information. The same applies when working with standard libraries.
Types ¶
type ErrGroup ¶ added in v0.2.1
type ErrGroup struct {
// contains filtered or unexported fields
}
ErrGroup - A ErrGroup is a collection of goroutines working on subtasks that are part of the same overall task.
A zero ErrGroup is valid and does not cancel on error.
Example (JustErrors) ¶
var g ErrGroup var urls = []string{ "http://baidu.com", "http://qq.com", "http://websitenotexist.com", } for _, url := range urls { // Launch a goroutine to fetch the URL. url := url // https://golang.org/doc/faq#closures_and_goroutines g.Go(func() error { // Fetch the URL. resp, err := http.Get(url) if err != nil { return errors.New("http get err") } resp.Body.Close() return nil }) } // Wait for all HTTP fetches to complete. err := g.Wait() fmt.Println(err)
Output: http get err
Example (Parallel) ¶
Google := func(ctx context.Context, query string) ([]Result, error) { g, ctx := NewErrGroup(ctx) searches := []Search{Web, Image, Video} results := make([]Result, len(searches)) for i, search := range searches { i, search := i, search // https://golang.org/doc/faq#closures_and_goroutines g.Go(func() error { result, err := search(ctx, query) if err == nil { results[i] = result } return err }) } if err := g.Wait(); err != nil { return nil, err } return results, nil } results, err := Google(context.Background(), "golang") if err != nil { fmt.Fprintln(os.Stderr, err) return } for _, result := range results { fmt.Println(result) }
Output: web result for "golang" image result for "golang" video result for "golang"
func NewErrGroup ¶ added in v0.2.1
NewErrGroup - NewErrGroup returns a new Group and an associated Context derived from ctx.
The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.