Commit 4869996b authored by Rob Pike's avatar Rob Pike

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
parent bbdd2070
......@@ -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 {
......
......@@ -928,7 +928,7 @@ func TestErrors(t *testing.T) {
},
{
`{{template "foo"}}`,
"z:1: no such template foo",
"z:1: no such template \"foo\"",
},
{
`<div{{template "y"}}>` +
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment