Refactor: Recover middleware now uses RenderError + add full notifications view
- Replaced http.Error with helpers.RenderError in Recover middleware - Custom 500.html now rendered with layout and topbar on panic - RenderError gracefully checks template existence and falls back to plain response - Added /account/notifications full view page (index) - Linked "Back to notifications" from notification read view - Fixed typo in template path for notifications/index.html - Improved layout consistency across error and account pages
This commit is contained in:
@@ -1,24 +1,40 @@
|
||||
package helpers
|
||||
|
||||
// ToDo should be a handler?
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"os"
|
||||
"synlotto-website/models"
|
||||
)
|
||||
|
||||
func RenderError(w http.ResponseWriter, r *http.Request, statusCode int) {
|
||||
log.Printf("⚙️ RenderError called with status: %d", statusCode)
|
||||
|
||||
context := TemplateContext(w, r, models.TemplateData{})
|
||||
|
||||
page := fmt.Sprintf("templates/error/%d.html", statusCode)
|
||||
tmpl := LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), page)
|
||||
pagePath := fmt.Sprintf("templates/error/%d.html", statusCode)
|
||||
log.Printf("📄 Checking for template file: %s", pagePath)
|
||||
|
||||
if _, err := os.Stat(pagePath); err != nil {
|
||||
log.Printf("🚫 Template file missing: %s", err)
|
||||
http.Error(w, http.StatusText(statusCode), statusCode)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("✅ Template file found, loading...")
|
||||
|
||||
tmpl := LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), pagePath)
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||
if err != nil {
|
||||
log.Printf("❌ Failed to render error page for %d: %v", statusCode, err)
|
||||
log.Printf("❌ Failed to render error page layout: %v", err)
|
||||
http.Error(w, http.StatusText(statusCode), statusCode)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("✅ Successfully rendered 500 page")
|
||||
}
|
||||
|
||||
//ToDo Pages.go /template.go to be merged?
|
||||
|
||||
Reference in New Issue
Block a user