Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

feat: support std errors functions #213

Merged
merged 13 commits into from
Jan 3, 2020
13 changes: 13 additions & 0 deletions go113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build go1.13

package errors

import (
stderrors "errors"
)

func Is(err, target error) bool { return stderrors.Is(err, target) }

func As(err error, target interface{}) bool { return stderrors.As(err, target) }

func Unwrap(err error) error { return stderrors.Unwrap(err) }
Sherlock-Holo marked this conversation as resolved.
Show resolved Hide resolved
146 changes: 143 additions & 3 deletions go113_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,154 @@
package errors
Sherlock-Holo marked this conversation as resolved.
Show resolved Hide resolved

import (
stdlib_errors "errors"
stderrors "errors"
"reflect"
"testing"
)

func TestErrorChainCompat(t *testing.T) {
err := stdlib_errors.New("error that gets wrapped")
err := stderrors.New("error that gets wrapped")
wrapped := Wrap(err, "wrapped up")
if !stdlib_errors.Is(wrapped, err) {
if !stderrors.Is(wrapped, err) {
t.Errorf("Wrap does not support Go 1.13 error chains")
}
}

func TestIs(t *testing.T) {
err := New("test")

type args struct {
err error
target error
}
tests := []struct {
name string
args args
want bool
}{
{
name: "with stack",
args: args{
err: WithStack(err),
target: err,
},
want: true,
},
{
name: "with message",
args: args{
err: WithMessage(err, "test"),
target: err,
},
want: true,
},
{
name: "with message format",
args: args{
err: WithMessagef(err, "%s", "test"),
target: err,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Is(tt.args.err, tt.args.target); got != tt.want {
t.Errorf("Is() = %v, want %v", got, tt.want)
}
})
}
}

type customErr struct {
msg string
}

func (c customErr) Error() string { return c.msg }

func TestAs(t *testing.T) {
var err = customErr{msg: "test message"}

type args struct {
err error
target interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "with stack",
args: args{
err: WithStack(err),
target: new(customErr),
Sherlock-Holo marked this conversation as resolved.
Show resolved Hide resolved
},
want: true,
},
{
name: "with message",
args: args{
err: WithMessage(err, "test"),
target: new(customErr),
},
want: true,
},
{
name: "with message format",
args: args{
err: WithMessagef(err, "%s", "test"),
target: new(customErr),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := As(tt.args.err, tt.args.target); got != tt.want {
t.Errorf("As() = %v, want %v", got, tt.want)
}

ce := tt.args.target.(*customErr)
if !reflect.DeepEqual(err, *ce) {
t.Errorf("set target error failed, target error is %v", *ce)
}
})
}
}

func TestUnwrap(t *testing.T) {
err := New("test")

type args struct {
err error
}
tests := []struct {
name string
args args
want error
}{
{
name: "with stack",
args: args{err: WithStack(err)},
want: err,
},
{
name: "with message",
args: args{err: WithMessage(err, "test")},
want: err,
},
{
name: "with message format",
args: args{err: WithMessagef(err, "%s", "test")},
want: err,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Unwrap(tt.args.err); !reflect.DeepEqual(err, tt.want) {
t.Errorf("Unwrap() error = %v, want %v", err, tt.want)
}
})
}
}