Fix linter errors: add error checking for template execution and file operations
SonarQube Scan / SonarQube Analysis (push) Failing after 1m12s

This commit is contained in:
2026-04-20 21:21:24 +01:00
parent 9bc39be5fa
commit fcfc9634ec
3 changed files with 30 additions and 13 deletions
+11 -7
View File
@@ -76,14 +76,18 @@ func handleSend(baseDir, apiToken, userKey string) http.HandlerFunc {
email := r.FormValue("email")
message := r.FormValue("message")
// Validate required fields
if name == "" || email == "" || message == "" {
data := map[string]string{
"status": "fail",
}
indexTemplate.Execute(w, data)
return
// Validate required fields
if name == "" || email == "" || message == "" {
data := map[string]string{
"status": "fail",
}
err = indexTemplate.Execute(w, data)
if err != nil {
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
return
}
// Send page via Pushover
success, err := sendPage(apiToken, userKey, name, email, message)
+18 -6
View File
@@ -14,7 +14,9 @@ func TestHandleIndexGET(t *testing.T) {
// Create a temporary directory with a test template
tmpDir := t.TempDir()
templatesDir := filepath.Join(tmpDir, "templates")
os.Mkdir(templatesDir, 0755)
if err := os.Mkdir(templatesDir, 0755); err != nil {
t.Fatalf("Failed to create templates directory: %v", err)
}
// Create a simple test template
templateContent := `
@@ -64,7 +66,9 @@ func TestHandleSendPOST(t *testing.T) {
// Create a temporary directory with a test template
tmpDir := t.TempDir()
templatesDir := filepath.Join(tmpDir, "templates")
os.Mkdir(templatesDir, 0755)
if err := os.Mkdir(templatesDir, 0755); err != nil {
t.Fatalf("Failed to create templates directory: %v", err)
}
// Create a simple test template
templateContent := `
@@ -79,7 +83,9 @@ func TestHandleSendPOST(t *testing.T) {
</html>
`
templatePath := filepath.Join(templatesDir, "index.html")
os.WriteFile(templatePath, []byte(templateContent), 0644)
if err := os.WriteFile(templatePath, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test template: %v", err)
}
// Clear the template cache before test
clearTemplateCache()
@@ -155,11 +161,15 @@ func TestHandleSendPOST(t *testing.T) {
func TestHandleSendInvalidForm(t *testing.T) {
tmpDir := t.TempDir()
templatesDir := filepath.Join(tmpDir, "templates")
os.Mkdir(templatesDir, 0755)
if err := os.Mkdir(templatesDir, 0755); err != nil {
t.Fatalf("Failed to create templates directory: %v", err)
}
templateContent := `<html><body>Test</body></html>`
templatePath := filepath.Join(templatesDir, "index.html")
os.WriteFile(templatePath, []byte(templateContent), 0644)
if err := os.WriteFile(templatePath, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test template: %v", err)
}
clearTemplateCache()
@@ -188,7 +198,9 @@ func TestTemplateCache(t *testing.T) {
templateContent := `<html><body>Cached</body></html>`
templatePath := filepath.Join(templatesDir, "index.html")
os.WriteFile(templatePath, []byte(templateContent), 0644)
if err := os.WriteFile(templatePath, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test template: %v", err)
}
clearTemplateCache()
+1
View File
@@ -23,6 +23,7 @@ func main() {
// Generate a random key if not provided (not recommended for production)
csrfKey = "dev-key-change-in-production"
}
_ = csrfKey // TODO: Use CSRF protection in handlers
// Set up paths relative to binary location
exePath, err := os.Executable()