simple error handling
alex wennerberg alex@alexwennerberg.com
Tue, 20 Oct 2020 22:26:14 -0700
2 files changed,
19 insertions(+),
3 deletions(-)
M
http.go
→
http.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 } }