2019-01-14 01:08:38 +00:00
|
|
|
package merr
|
|
|
|
|
|
|
|
import (
|
2019-02-09 19:08:30 +00:00
|
|
|
"context"
|
2019-01-14 01:08:38 +00:00
|
|
|
"errors"
|
2021-04-09 23:30:59 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mtest/massert"
|
2019-01-14 01:08:38 +00:00
|
|
|
)
|
|
|
|
|
2021-04-10 00:05:07 +00:00
|
|
|
func TestFullError(t *testing.T) {
|
2021-04-09 23:30:59 +00:00
|
|
|
massert.Require(t, massert.Nil(Wrap(context.Background(), nil)))
|
2019-02-27 18:05:51 +00:00
|
|
|
|
|
|
|
ctx := mctx.Annotate(context.Background(),
|
2019-02-09 19:08:30 +00:00
|
|
|
"a", "aaa aaa\n",
|
|
|
|
"c", "ccc\nccc\n",
|
2019-02-27 18:05:51 +00:00
|
|
|
"d\t", "weird key but ok")
|
|
|
|
|
|
|
|
{
|
2021-04-09 23:30:59 +00:00
|
|
|
e := New(ctx, "foo")
|
2019-02-27 18:05:51 +00:00
|
|
|
exp := `foo
|
|
|
|
* a: aaa aaa
|
|
|
|
* c:
|
|
|
|
ccc
|
|
|
|
ccc
|
|
|
|
* d: weird key but ok
|
2021-04-09 23:30:59 +00:00
|
|
|
* line: merr/merr_test.go:22`
|
2021-04-10 00:05:07 +00:00
|
|
|
massert.Require(t, massert.Equal(exp, e.(Error).FullError()))
|
2019-02-27 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-04-09 23:30:59 +00:00
|
|
|
e := Wrap(ctx, errors.New("foo"))
|
2019-02-27 18:05:51 +00:00
|
|
|
exp := `foo
|
2019-01-14 01:08:38 +00:00
|
|
|
* a: aaa aaa
|
|
|
|
* c:
|
|
|
|
ccc
|
|
|
|
ccc
|
2019-02-09 19:08:30 +00:00
|
|
|
* d: weird key but ok
|
2021-04-09 23:30:59 +00:00
|
|
|
* line: merr/merr_test.go:34`
|
2021-04-10 00:05:07 +00:00
|
|
|
massert.Require(t, massert.Equal(exp, e.(Error).FullError()))
|
2019-02-27 18:05:51 +00:00
|
|
|
}
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 00:05:07 +00:00
|
|
|
func TestAsIsError(t *testing.T) {
|
|
|
|
testST := newStacktrace(0)
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
ctxA := mctx.Annotate(context.Background(), "a", "1")
|
|
|
|
ctxB := mctx.Annotate(context.Background(), "b", "2")
|
|
|
|
errFoo := errors.New("foo")
|
|
|
|
|
|
|
|
type test struct {
|
2021-04-10 00:05:07 +00:00
|
|
|
in error
|
|
|
|
expAs error
|
|
|
|
expIs error
|
|
|
|
expStr string
|
2021-04-09 23:30:59 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
tests := []test{
|
|
|
|
{
|
2021-04-10 00:05:07 +00:00
|
|
|
in: nil,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
{
|
2021-04-10 00:05:07 +00:00
|
|
|
in: errors.New("bar"),
|
|
|
|
expStr: "bar",
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
expAs: Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
2021-04-10 00:05:07 +00:00
|
|
|
expIs: errFoo,
|
|
|
|
expStr: "foo",
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: fmt.Errorf("bar: %w", Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
}),
|
|
|
|
expAs: Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
2021-04-10 00:05:07 +00:00
|
|
|
expIs: errFoo,
|
|
|
|
expStr: "bar: foo",
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Wrap(ctxB, Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
}),
|
|
|
|
expAs: Error{
|
|
|
|
Err: Error{
|
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
Ctx: mctx.MergeAnnotations(ctxA, ctxB),
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
2021-04-10 00:05:07 +00:00
|
|
|
expIs: errFoo,
|
|
|
|
expStr: "foo",
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
{
|
2021-04-10 00:05:07 +00:00
|
|
|
in: Wrap(ctxB, fmt.Errorf("bar: %w", Error{
|
2021-04-09 23:30:59 +00:00
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
})),
|
|
|
|
expAs: Error{
|
2021-04-10 00:05:07 +00:00
|
|
|
Err: fmt.Errorf("bar: %w", Error{
|
2021-04-09 23:30:59 +00:00
|
|
|
Err: errFoo,
|
|
|
|
Ctx: ctxA,
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
}),
|
|
|
|
Ctx: mctx.MergeAnnotations(ctxA, ctxB),
|
2021-04-10 00:05:07 +00:00
|
|
|
Stacktrace: testST,
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
2021-04-10 00:05:07 +00:00
|
|
|
expIs: errFoo,
|
|
|
|
expStr: "bar: foo",
|
2021-04-09 23:30:59 +00:00
|
|
|
},
|
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
|
|
|
var in Error
|
|
|
|
ok := errors.As(test.in, &in)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
massert.Require(t, massert.Comment(
|
|
|
|
massert.Equal(test.expAs != nil, ok),
|
|
|
|
"test.in:%#v ok:%v", test.in, ok,
|
|
|
|
))
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
if test.expAs == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
expAs := test.expAs.(Error)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
inAA := mctx.EvaluateAnnotations(in.Ctx, nil)
|
|
|
|
expAsAA := mctx.EvaluateAnnotations(expAs.Ctx, nil)
|
|
|
|
in.Ctx = nil
|
|
|
|
expAs.Ctx = nil
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
massert.Require(t,
|
|
|
|
massert.Equal(expAsAA, inAA),
|
|
|
|
massert.Equal(expAs, in),
|
|
|
|
massert.Comment(
|
|
|
|
massert.Equal(true, errors.Is(test.in, test.expIs)),
|
|
|
|
"errors.Is(\ntest.in:%#v,\ntest.expIs:%#v,\n)", test.in, test.expIs,
|
|
|
|
),
|
2021-04-10 00:05:07 +00:00
|
|
|
massert.Equal(test.expStr, test.in.Error()),
|
2021-04-09 23:30:59 +00:00
|
|
|
)
|
|
|
|
})
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
}
|