all repos — flounder @ d865a8dbc32bcff967a9cf8733d2eda2f36f4889

A small site builder for the Gemini protocol

simple error handling
alex wennerberg alex@alexwennerberg.com
Tue, 20 Oct 2020 22:26:14 -0700
commit

d865a8dbc32bcff967a9cf8733d2eda2f36f4889

parent

19944763fc195d2db224147841ebcbf2aafb435d

2 files changed, 19 insertions(+), 3 deletions(-)

jump to
M http.gohttp.go

@@ -14,14 +14,28 @@ Domain string

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 @@ Users []string

}{h.Domain, h.SiteTitle, indexFiles, allUsers} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { - log.Fatal(err) + renderError(w, InternalServerError) + return } }
A templates/error.html

@@ -0,0 +1,1 @@

+{{ .ErrorMsg }}