From: Rob Pike Date: Tue, 20 Dec 2011 20:58:23 +0000 (-0800) Subject: template: better error message for empty templates X-Git-Tag: weekly.2011-12-22~92 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4869996b928d0e5dc978a4de31f2824b47ae8cb7;p=gostls13.git 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 --- diff --git a/src/pkg/html/template/escape.go b/src/pkg/html/template/escape.go index 2f6be3b6c2..c6f723ae4a 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 7702300ffd..a57f9826b5 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 ba20fff89d..acb88afee3 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