mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-30 20:54:04 +08:00
Better error handling when executing templates
This commit is contained in:
parent
0cbaed2443
commit
bdd145b0de
|
@ -3,6 +3,7 @@
|
|||
package browse
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
|
@ -122,8 +123,6 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
files, err := file.Readdir(-1)
|
||||
if err != nil {
|
||||
return http.StatusForbidden, err
|
||||
|
@ -182,11 +181,14 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
Items: fileinfos,
|
||||
}
|
||||
|
||||
// TODO: Don't write to w until we know there wasn't an error
|
||||
err = bc.Template.Execute(w, listing)
|
||||
// TODO: Use pooled buffers to reduce allocations
|
||||
var buf bytes.Buffer
|
||||
err = bc.Template.Execute(&buf, listing)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
buf.WriteTo(w)
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package templates
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"path"
|
||||
"text/template"
|
||||
|
@ -47,10 +48,13 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
}
|
||||
|
||||
// Execute it
|
||||
err = tpl.Execute(w, ctx)
|
||||
// TODO: Use pooled buffers to reduce allocations
|
||||
var buf bytes.Buffer
|
||||
err = bc.Template.Execute(&buf, ctx)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
buf.WriteTo(w)
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user