Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
template: fix error checking on execute without parse
Browse files Browse the repository at this point in the history
Fixed error checking in exec.go to give a sensible error message when
execution is attempted before a successful parse (rather than an
outright panic).

R=r
CC=golang-dev
https://golang.org/cl/5306065
  • Loading branch information
bytbox authored and robpike committed Oct 31, 2011
1 parent 92926f5 commit cae23f0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/pkg/exp/template/html/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,8 @@ func TestEnsurePipelineContains(t *testing.T) {
}
}

func expectExecuteFailure(t *testing.T, b *bytes.Buffer) {
if x := recover(); x != nil {
func expectExecuteFailure(t *testing.T, b *bytes.Buffer, err os.Error) {
if err != nil {
if b.Len() != 0 {
t.Errorf("output on buffer: %q", b.String())
}
Expand All @@ -1563,8 +1563,8 @@ func TestEscapeErrorsNotIgnorable(t *testing.T) {
var b bytes.Buffer
tmpl := template.Must(template.New("dangerous").Parse("<a"))
Escape(tmpl)
defer expectExecuteFailure(t, &b)
tmpl.Execute(&b, nil)
err := tmpl.Execute(&b, nil)
expectExecuteFailure(t, &b, err)
}

func TestEscapeSetErrorsNotIgnorable(t *testing.T) {
Expand All @@ -1574,8 +1574,8 @@ func TestEscapeSetErrorsNotIgnorable(t *testing.T) {
}
EscapeSet(s, "t")
var b bytes.Buffer
defer expectExecuteFailure(t, &b)
s.Execute(&b, "t", nil)
err = s.Execute(&b, "t", nil)
expectExecuteFailure(t, &b, err)
}

func TestRedundantFuncs(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/template/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) {
line: 1,
vars: []variable{{"$", value}},
}
if t.Root == nil {
if t.Tree == nil || t.Root == nil {
state.errorf("must be parsed before execution")
}
state.walk(value, t.Root)
Expand Down

0 comments on commit cae23f0

Please sign in to comment.