Skip to content

Commit

Permalink
copy from e4
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Sep 2, 2022
1 parent 26ab014 commit d075cb7
Show file tree
Hide file tree
Showing 28 changed files with 1,584 additions and 0 deletions.
13 changes: 13 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package e5

import (
"fmt"
)

type (
any = interface{}
)

var (
pt = fmt.Printf
)
78 changes: 78 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package e5

import (
"errors"
"io"
"testing"
)

var testErr error

func BenchmarkHandleNil(b *testing.B) {
for i := 0; i < b.N; i++ {
err := func() (err error) {
defer Handle(&err)
return nil
}()
if err != nil {
b.Fatal()
}
}
}

func BenchmarkHandleErr(b *testing.B) {
for i := 0; i < b.N; i++ {
err := func() (err error) {
defer Handle(&err)
return io.EOF
}()
if !errors.Is(err, io.EOF) {
b.Fatal()
}
}
}

func BenchmarkHandleCheckNil(b *testing.B) {
for i := 0; i < b.N; i++ {
err := func() (err error) {
defer Handle(&err)
Check(nil)
return
}()
if err != nil {
b.Fatal()
}
}
}

func BenchmarkHandleCheck(b *testing.B) {
for i := 0; i < b.N; i++ {
err := func() (err error) {
defer Handle(&err)
Check(io.EOF)
return
}()
if !errors.Is(err, io.EOF) {
b.Fatal()
}
}
}

func BenchmarkHandleCheckDeep(b *testing.B) {
var fn func(int) error
fn = func(i int) (err error) {
defer Handle(&err)
if i == 0 {
return io.EOF
}
Check(fn(i - 1))
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := fn(30)
if !errors.Is(err, io.EOF) {
b.Fatal()
}
}
}
27 changes: 27 additions & 0 deletions check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package e5

// CheckFunc is the type of Check, see Check's doc for details
type CheckFunc func(err error, args ...error) error

// Check is for error checking.
// if err is not nil, it will be wrapped by DefaultWrap then raised by Throw
var Check = CheckFunc(func(err error, args ...error) error {
if err == nil {
return nil
}
err = Wrap.With(args...)(err)
return Throw(err)
})

// With returns a new CheckFunc that do additional wrapping with moreWraps
func (c CheckFunc) With(moreArgs ...error) CheckFunc {
return func(err error, args ...error) error {
if err == nil {
return nil
}
err = Wrap.With(args...)(err)
return c(err, moreArgs...)
}
}

var CheckWithStacktrace = Check.With(WrapStacktrace)
Loading

0 comments on commit d075cb7

Please sign in to comment.