flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit d865a8dbc32bcff967a9cf8733d2eda2f36f4889
parent 19944763fc195d2db224147841ebcbf2aafb435d
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Tue, 20 Oct 2020 22:26:14 -0700

simple error handling

Diffstat:
Mhttp.go | 21++++++++++++++++++---
Atemplates/error.html | 1+
2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/http.go b/http.go @@ -14,14 +14,28 @@ type IndexHandler struct { SiteTitle string } +const InternalServerError = "500: Internal Server Error" + +func renderError(w http.ResponseWriter, errorMsg string) { // TODO think about pointers + w.WriteHeader(http.StatusInternalServerError) + log.Println(errorMsg) + data := struct{ ErrorMsg string }{errorMsg} + err := t.ExecuteTemplate(w, "error.html", data) + if err != nil { // shouldn't happen probably + fmt.Fprintf(w, errorMsg) + } +} + func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { indexFiles, err := getIndexFiles() if err != nil { - log.Fatal(err) + renderError(w, InternalServerError) + return } allUsers, err := getUsers() if err != nil { - log.Fatal(err) + renderError(w, InternalServerError) + return } data := struct { Domain string @@ -31,7 +45,8 @@ func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { }{h.Domain, h.SiteTitle, indexFiles, allUsers} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { - log.Fatal(err) + renderError(w, InternalServerError) + return } } diff --git a/templates/error.html b/templates/error.html @@ -0,0 +1 @@ +{{ .ErrorMsg }}