From ff99f6466ff82b4aff0a95e250b2e1d3824644e2 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 14 May 2020 14:41:45 -0400 Subject: [PATCH] Handle error from template.Execute Refactored the function to make the problem more obvious, by using a guard. --- agent/http.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/agent/http.go b/agent/http.go index 72a68170c..099e38a56 100644 --- a/agent/http.go +++ b/agent/http.go @@ -169,20 +169,21 @@ type templatedIndexFS struct { func (fs *templatedIndexFS) Open(name string) (http.File, error) { file, err := fs.fs.Open(name) - if err == nil && name == "/index.html" { - content, _ := ioutil.ReadAll(file) - file.Seek(0, 0) - t, err := template.New("fmtedindex").Parse(string(content)) - if err != nil { - return nil, err - } - var out bytes.Buffer - err = t.Execute(&out, fs.templateVars()) - - file = newTemplatedFile(&out, file) + if err != nil || name != "/index.html" { + return file, err } - return file, err + content, _ := ioutil.ReadAll(file) + file.Seek(0, 0) + t, err := template.New("fmtedindex").Parse(string(content)) + if err != nil { + return nil, err + } + var out bytes.Buffer + if err := t.Execute(&out, fs.templateVars()); err != nil { + return nil, err + } + return newTemplatedFile(&out, file), nil } // endpoint is a Consul-specific HTTP handler that takes the usual arguments in