err:=tmpl.ExecuteTemplate(writer,"page.html",data)iferr!=nil{h.log.Error("template execution failed","error",err)http.Error(writer,"Internal Server Error",http.StatusInternalServerError)}
The problem: ExecuteTemplate writes directly to the http.ResponseWriter. If the template partially renders before hitting an error, the HTTP 200 status and partial HTML have already been sent. The subsequent http.Error() call:
Cannot change the already-sent 200 status code
Appends "Internal Server Error" text to the partial HTML
Results in a corrupt, half-rendered page
Suggested Fix
Render templates to a buffer first, then write to the response only on success:
varbufbytes.Buffererr:=tmpl.ExecuteTemplate(&buf,"page.html",data)iferr!=nil{h.log.Error("template execution failed","error",err)http.Error(writer,"Internal Server Error",http.StatusInternalServerError)return}buf.WriteTo(writer)
This is a common Go web pattern. Consider creating a helper method on Handlers.
## Severity: MEDIUM
## Files: Multiple handlers (setup.go, app.go, auth.go, dashboard.go)
## Description
In all handlers, the pattern is:
```go
err := tmpl.ExecuteTemplate(writer, "page.html", data)
if err != nil {
h.log.Error("template execution failed", "error", err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
}
```
The problem: `ExecuteTemplate` writes directly to the `http.ResponseWriter`. If the template partially renders before hitting an error, the HTTP 200 status and partial HTML have already been sent. The subsequent `http.Error()` call:
1. Cannot change the already-sent 200 status code
2. Appends "Internal Server Error" text to the partial HTML
3. Results in a corrupt, half-rendered page
## Suggested Fix
Render templates to a buffer first, then write to the response only on success:
```go
var buf bytes.Buffer
err := tmpl.ExecuteTemplate(&buf, "page.html", data)
if err != nil {
h.log.Error("template execution failed", "error", err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
return
}
buf.WriteTo(writer)
```
This is a common Go web pattern. Consider creating a helper method on Handlers.
Severity: MEDIUM
Files: Multiple handlers (setup.go, app.go, auth.go, dashboard.go)
Description
In all handlers, the pattern is:
The problem:
ExecuteTemplatewrites directly to thehttp.ResponseWriter. If the template partially renders before hitting an error, the HTTP 200 status and partial HTML have already been sent. The subsequenthttp.Error()call:Suggested Fix
Render templates to a buffer first, then write to the response only on success:
This is a common Go web pattern. Consider creating a helper method on Handlers.
do it, using the helper method. apply it in all places this pattern appears. create a PR.