errors

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: MIT Imports: 6 Imported by: 6

README

errors

Replacement for golang errors package.

Benchmarks

Take all benchmarks with a bucket of salt.

go test -bench=. -benchmem -count=1 -parallel 8 ./...

goos: linux
goarch: amd64
pkg: github.com/wspowell/errors
cpu: AMD Ryzen 9 4900HS with Radeon Graphics         
BenchmarkErrorsNew-8                    301735152                3.890 ns/op           0 B/op          0 allocs/op
BenchmarkErrorsWithStackTrace-8          1283198               929.4 ns/op           284 B/op          3 allocs/op
BenchmarkErrorsNewFmt-8                 16286179                69.42 ns/op            4 B/op          1 allocs/op
BenchmarkGoerrorsNew-8                  46670034                25.19 ns/op           16 B/op          1 allocs/op
BenchmarkGoerrorsWrap-8                  6644190               169.2 ns/op            36 B/op          2 allocs/op
BenchmarkErrorString-8                  1000000000               0.7221 ns/op          0 B/op          0 allocs/op
BenchmarkErrorStringWithStackTrace-8      416150              3012 ns/op             400 B/op          9 allocs/op
PASS
ok      github.com/wspowell/errors      11.682s

goos: linux
goarch: amd64
pkg: github.com/wspowell/errors/result
cpu: AMD Ryzen 9 4900HS with Radeon Graphics         
BenchmarkResultOkInt-8            328060              3646 ns/op               0 B/op          0 allocs/op
BenchmarkResultErrInt-8           326503              3643 ns/op               0 B/op          0 allocs/op
BenchmarkGoerrorOkInt-8           244785              4864 ns/op               0 B/op          0 allocs/op
BenchmarkGoerrorErrInt-8          501597              2488 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/wspowell/errors/result       4.987s

Documentation

Overview

Copied (and modified) from: github.com/pkg/errors@v0.9.1/stack.go

Index

Constants

View Source
const None = ""

Variables

View Source
var ErrNone = Error{/* contains filtered or unexported fields */}

Functions

func As

func As[T ~string](err Error) T

As the sentinel type for the error.

This allows Error to be used in a switch in conjunction with linter "exhaustive".

For example:

type myError errors.Sentinel

const (
  errOne   = myError("one")
  errTwo   = myError("two")
  errThree = myError("three")
)

func multipleErrors(ctx context.Context, err int) errors.Error {
  switch err {
  case 1:
    return errors.New(ctx, errOne)
  case 2:
    return errors.New(ctx, errTwo)
  case 3:
    return errors.New(ctx, errThree)
  default:
    return errors.ErrNone
  }
}

func TestSentinelEnum(t *testing.T) {
  t.Parallel()

  err := multipleErrors(context.Background(), 1)
  //nolint:exhaustive // reason: expected lint error for testing
  switch errors.As[myError](err) {
  case errOne:
    // Expected case.
  default:
    assert.Fail(t, "expected errOne")
  }
}

func WithStackTrace added in v0.4.0

func WithStackTrace(ctx context.Context) context.Context

Types

type Error added in v0.4.0

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

Error is an instance of a sentinel error. If the error was created via New() then the error is considered and inline error and is not comparable with another Error.

Error should not be used with "==". Instead, use Error.As().

func Catch added in v0.2.0

func Catch(ctx context.Context, fn func(ctx context.Context)) (err Error)

Catch potential panics that occur in a function call. Panics should never occur, therefore stack traces print regardless of build mode (release or debug).

func New

func New[T ~string](ctx context.Context, format T, values ...any) Error

New error instance.

This should be called when the application creates a new error. This new Error is not (intended to be) comparable with other Errors and therefore cannot be used as a sentinel error. A Sentinel may be used and compared using Error.Sentinel(). A context is accepted in order to pass through API level feature flags.

func Recover added in v0.2.0

func Recover(ctx context.Context, errPanic any) Error

func (Error) Error added in v0.5.0

func (self Error) Error() string

Error string, ignoring any call stack.

func (Error) Format added in v0.4.0

func (self Error) Format(state fmt.State, verb rune)

func (Error) IsNone added in v0.4.0

func (self Error) IsNone() bool

IsNone returns true if the Error is zero value.

It is recommended to use Error along with Result and instead use Result.IsOk().

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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