nerrors

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInternal -
	ErrInternal = NewBizError(-1, "internal service error")
	// ErrUnauthorized -
	ErrUnauthorized = NewBizError(-2, "unauthorized")
	// ErrForbidden -
	ErrForbidden = NewBizError(-3, "forbidden")
)

Functions

func Append added in v0.1.2

func Append(left error, right error) error

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

func Combine(errors ...error) error

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

func Errorf(format string, args ...interface{}) error

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

func New(message string) error

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

func WithStack(err error) error

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

func Wrap(err error, message string) error

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

func Wrapf(err error, format string, args ...interface{}) error

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 BizError

type BizError interface {
	error
	Code() int
	Msg() string
	New(message string) BizError
}

BizError -

func NewBizError

func NewBizError(code int16, msg string) BizError

NewBizError -

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

func NewErrGroup(ctx context.Context) (*ErrGroup, context.Context)

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.

func (*ErrGroup) Go added in v0.2.1

func (g *ErrGroup) Go(f func() error)

Go - Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*ErrGroup) Wait added in v0.2.1

func (g *ErrGroup) Wait() error

Wait - Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

Jump to

Keyboard shortcuts

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