package template import ( "errors" "strings" "testing" ) func TestTemplateError(t *testing.T) { cause := errors.New("boom") tests := []struct { name string err *TemplateError phase string }{ {"load", NewTemplateLoadError("cannot read", cause), "load"}, {"parse", NewTemplateParseError("bad syntax", cause), "parse"}, {"execute", NewTemplateExecuteError("failed render", cause), "execute"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.err.Phase != tt.phase { t.Errorf("phase = %q", tt.err.Phase) } msg := tt.err.Error() if !strings.Contains(msg, "template "+tt.phase+" error") || !strings.Contains(msg, "boom") { t.Errorf("message = %q", msg) } if !errors.Is(tt.err, cause) { t.Error("errors.Is must reach cause") } var te *TemplateError if !errors.As(error(tt.err), &te) || te != tt.err { t.Error("errors.As failed") } }) } } func TestTemplateErrorWithoutCause(t *testing.T) { e := NewTemplateParseError("only message", nil) if got := e.Error(); got != "template parse error: only message" { t.Errorf("got %q", got) } if e.Unwrap() != nil { t.Error("Unwrap must be nil") } }