Esempio n. 1
0
func TestCause(t *testing.T) {
	if cause := errors.Cause(someErr); cause != someErr {
		t.Fatalf("expected %q kind; got %#v", someErr, cause)
	}
	causeErr := errors.New("cause error")
	underlyingErr := errors.New("underlying error")                 //err TestCause#1
	err := errors.WithCausef(underlyingErr, causeErr, "foo %d", 99) //err TestCause#2
	if errors.Cause(err) != causeErr {
		t.Fatalf("expected %q; got %#v", causeErr, errors.Cause(err))
	}
	checkErr(t, err, underlyingErr, "foo 99: underlying error", "[{$TestCause#2$: foo 99} {$TestCause#1$: underlying error}]", causeErr)
	err = &embed{err.(*errors.Err)}
	if errors.Cause(err) != causeErr {
		t.Fatalf("expected %q; got %#v", causeErr, errors.Cause(err))
	}
}
Esempio n. 2
0
func checkErr(t *testing.T, err, underlying error, msg string, details string, cause error) {
	if err == nil {
		t.Fatalf("err is nil; want %q", msg)
	}
	if err.Error() != msg {
		t.Fatalf("unexpected message: want %q; got %q", msg, err.Error())
	}
	if err, ok := err.(errors.Wrapper); ok {
		if err.Underlying() != underlying {
			t.Fatalf("unexpected underlying error: want %q; got %v", underlying, err.Underlying())
		}
	} else if underlying != nil {
		t.Fatalf("no underlying error found; want %q", underlying)
	}
	if errors.Cause(err) != cause {
		t.Fatalf("unexpected cause: want %#v; got %#v", cause, errors.Cause(err))
	}
	wantDetails := replaceLocations(details)
	if gotDetails := errors.Details(err); gotDetails != wantDetails {
		t.Fatalf("unexpected details: want %q; got %q", wantDetails, gotDetails)
	}
}
Esempio n. 3
0
func TestMaskFunc(t *testing.T) {
	err0 := errors.New("zero")
	err1 := errors.New("one")

	allowVals := func(vals ...error) (r []func(error) bool) {
		for _, val := range vals {
			r = append(r, errors.Is(val))
		}
		return
	}
	tests := []struct {
		err    error
		allow0 []func(error) bool
		allow1 []func(error) bool
		cause  error
	}{{
		err:    err0,
		allow0: allowVals(err0),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0),
		cause:  nil,
	}, {
		err:    err0,
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err1),
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0, err1),
		cause:  err1,
	}, {
		err:    err0,
		allow1: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow1: allowVals(err0, err1),
		cause:  err1,
	}}
	for i, test := range tests {
		wrap := errors.MaskFunc(test.allow0...)
		err := wrap(test.err, test.allow1...)
		cause := errors.Cause(err)
		wantCause := test.cause
		if wantCause == nil {
			wantCause = err
		}
		if cause != wantCause {
			t.Errorf("test %d. got %#v want %#v", i, cause, err)
		}
	}
}