mirror of
https://github.com/golang/go
synced 2024-11-21 23:14:40 -07:00
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
This commit is contained in:
parent
bbdd2070a9
commit
4869996b92
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user