all repos — flounder @ 3c696860461d44ec54c7a718b3e5221705c5e2d1

A small site builder for the Gemini protocol

cleanup error handling
alex wennerberg alex@alexwennerberg.com
Tue, 20 Oct 2020 22:45:39 -0700
commit

3c696860461d44ec54c7a718b3e5221705c5e2d1

parent

35f0a3f55d49b0043cb33279ea6867ec32362e22

1 files changed, 8 insertions(+), 9 deletions(-)

jump to
M http.gohttp.go

@@ -15,13 +15,13 @@ SiteTitle string

} // TODO somewhat better error handling -const InternalServerError = "500: Internal Server Error" +const InternalServerErrorMsg = "500: Internal Server Error" -func renderError(w http.ResponseWriter, errorMsg string) { // TODO think about pointers +func renderError(w http.ResponseWriter, errorMsg string, statusCode int) { // TODO think about pointers data := struct{ ErrorMsg string }{errorMsg} err := t.ExecuteTemplate(w, "error.html", data) if err != nil { // shouldn't happen probably - fmt.Fprintf(w, errorMsg) + http.Error(w, errorMsg, statusCode) } }

@@ -29,14 +29,13 @@ func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

indexFiles, err := getIndexFiles() if err != nil { log.Println(err) - w.WriteHeader(http.StatusInternalServerError) - renderError(w, InternalServerError) + renderError(w, InternalServerErrorMsg, 500) return } allUsers, err := getUsers() if err != nil { - w.WriteHeader(http.StatusInternalServerError) - renderError(w, InternalServerError) + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) return } data := struct {

@@ -47,8 +46,8 @@ Users []string

}{h.Domain, h.SiteTitle, indexFiles, allUsers} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - renderError(w, InternalServerError) + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) return }