From 4869996b928d0e5dc978a4de31f2824b47ae8cb7 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 20 Dec 2011 12:58:23 -0800 Subject: [PATCH] template: better error message for empty templates New("x").ParseFiles("y") can result in an empty "x" template. Make the message clearer that this is the problem. The error returns from both template packages in this case were confusing. I considered making the method use "x" instead of "y" in this case, but that just made other situations confusing and harder to explain. Fixes #2594. R=golang-dev, rsc, r CC=golang-dev https://golang.org/cl/5498048 --- src/pkg/html/template/escape.go | 10 +++++++++- src/pkg/html/template/escape_test.go | 2 +- src/pkg/text/template/exec.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/pkg/html/template/escape.go b/src/pkg/html/template/escape.go index 2f6be3b6c21..c6f723ae4a4 100644 --- a/src/pkg/html/template/escape.go +++ b/src/pkg/html/template/escape.go @@ -486,9 +486,17 @@ func (e *escaper) escapeTree(c context, name string, line int) (context, string) } t := e.template(name) if t == nil { + // Two cases: The template exists but is empty, or has never been mentioned at + // all. Distinguish the cases in the error messages. + if e.tmpl.set[name] != nil { + return context{ + state: stateError, + err: errorf(ErrNoSuchTemplate, line, "%q is an incomplete or empty template", name), + }, dname + } return context{ state: stateError, - err: errorf(ErrNoSuchTemplate, line, "no such template %s", name), + err: errorf(ErrNoSuchTemplate, line, "no such template %q", name), }, dname } if dname != name { diff --git a/src/pkg/html/template/escape_test.go b/src/pkg/html/template/escape_test.go index 7702300ffda..a57f9826b5b 100644 --- a/src/pkg/html/template/escape_test.go +++ b/src/pkg/html/template/escape_test.go @@ -928,7 +928,7 @@ func TestErrors(t *testing.T) { }, { `{{template "foo"}}`, - "z:1: no such template foo", + "z:1: no such template \"foo\"", }, { `` + diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index ba20fff89df..acb88afee36 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -107,7 +107,7 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { vars: []variable{{"$", value}}, } if t.Tree == nil || t.Root == nil { - state.errorf("must be parsed before execution") + state.errorf("%q is an incomplete or empty template", t.name) } state.walk(value, t.Root) return