Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pprishchepa committed Jul 24, 2024
1 parent 54b10f3 commit 1198d8b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion easyerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func Join(errs ...error) error {
}

func Unwrap(err error) error {
return goerrors.Join(err)
return goerrors.Unwrap(err)
}
6 changes: 3 additions & 3 deletions easyerrors_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"testing"
)

const nestedLevels = 10
const nestedCalls = 10

var err error

func BenchmarkStdErrors(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = doStdCall(stderrors.New("foo"), nestedLevels)
err = doStdCall(stderrors.New("foo"), nestedCalls)
}
}

func BenchmarkEasyErrors(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = doEasyCall(New("foo"), nestedLevels)
err = doEasyCall(New("foo"), nestedCalls)
}
}

Expand Down
54 changes: 54 additions & 0 deletions easyerrors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package easyerrors

import "testing"
import "github.com/stretchr/testify/require"

//goland:noinspection GoPrintFunctions
func TestAs(t *testing.T) {
type fooable interface {
Foo()
}

err := Errorf("bar: %w", errFoo{})

var errHello fooable
require.True(t, As(err, &errHello))
require.False(t, As(New("bar"), &errHello))
}

//goland:noinspection GoDfaNilDereference,GoPrintFunctions
func TestErrorf(t *testing.T) {
err := New("foo")
err = Errorf("bar: %w", err)
require.Equal(t, "bar: foo", err.Error())
}

//goland:noinspection GoPrintFunctions
func TestIs(t *testing.T) {
var errFoo = New("foo")
err := Errorf("bar: %w", errFoo)
require.True(t, Is(err, errFoo))
require.False(t, Is(New("baz"), errFoo))
}

func TestJoin(t *testing.T) {
require.Equal(t, "foo\nbar", Join(New("foo"), New("bar")).Error())
}

func TestNew(t *testing.T) {
require.Equal(t, "foo", New("foo").Error())
}

//goland:noinspection GoPrintFunctions
func TestUnwrap(t *testing.T) {
err = Errorf("bar: %w", New("foo"))
err = Unwrap(err)
err = Unwrap(err)
require.Equal(t, "foo", err.Error())
}

type errFoo struct{}

func (e errFoo) Foo() {}

func (e errFoo) Error() string { return "foo" }
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/pprishchepa/easyerrors

go 1.22.5

require github.com/go-errors/errors v1.5.1
require (
github.com/go-errors/errors v1.5.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 1198d8b

Please sign in to comment.