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
140 changes: 140 additions & 0 deletions go113_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// +build go1.13
Sherlock-Holo marked this conversation as resolved.
Show resolved Hide resolved

package errors
Sherlock-Holo marked this conversation as resolved.
Show resolved Hide resolved

import (
"reflect"
"testing"
)

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{}

func (customErr) Error() string { return "" }

func TestAs(t *testing.T) {
var err customErr

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)
}
})
}
}

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)
}
})
}
}
13 changes: 13 additions & 0 deletions goold.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !go1.13

package errors

import (
"golang.org/x/xerrors"
)

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

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

func Unwrap(err error) error { return xerrors.Unwrap(err) }
140 changes: 140 additions & 0 deletions goold_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// +build !go1.13

package errors

import (
"reflect"
"testing"
)

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{}

func (customErr) Error() string { return "" }

func TestAs(t *testing.T) {
var err customErr

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),
},
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)
}
})
}
}

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)
}
})
}
}