Beispiel #1
0
func TestDetails(t *testing.T) {
	if details := errors.Details(nil); details != "[]" {
		t.Fatalf("errors.Details(nil) got %q want %q", details, "[]")
	}

	otherErr := fmt.Errorf("other")
	checkErr(t, otherErr, nil, "other", "[{other}]", otherErr)

	err0 := &embed{errors.New("foo").(*errors.Err)} //err TestStack#0
	checkErr(t, err0, nil, "foo", "[{$TestStack#0$: foo}]", err0)

	err1 := &embed{errors.Notef(err0, "bar").(*errors.Err)} //err TestStack#1
	checkErr(t, err1, err0, "bar: foo", "[{$TestStack#1$: bar} {$TestStack#0$: foo}]", err1)

	err2 := errors.Mask(err1) //err TestStack#2
	checkErr(t, err2, err1, "bar: foo", "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]", err2)
}
Beispiel #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)
	}
}