make HTTP error an interface
7 files changed, 86 insertions(+), 65 deletions(-)
M domain/content/publisher/mux.go → domain/content/publisher/mux.go
@@ -8,22 +8,19 @@ "alin.ovh/homestead/domain/web/templates" ihttp "alin.ovh/homestead/shared/http" ) -func (app *App) Index(w http.ResponseWriter, _ *http.Request) *ihttp.Error { +func (app *App) Index(w http.ResponseWriter, _ *http.Request) ihttp.Error { err := pubtpl.IndexPage(app.siteSettings, templates.PageSettings{ Title: "Home", }).Render(w) if err != nil { - return &ihttp.Error{ - Code: http.StatusInternalServerError, - Message: "Failed to render index page", - } + return ihttp.InternalServerError("Failed to render index page", err) } return nil } -func (app *App) Style(w http.ResponseWriter, r *http.Request) *ihttp.Error { +func (app *App) Style(w http.ResponseWriter, r *http.Request) ihttp.Error { w.Header().Set("Content-Type", "text/css") http.ServeFileFS(w, r, templates.Files, "style.css")
M domain/identity/webfinger/service.go → domain/identity/webfinger/service.go
@@ -51,7 +51,7 @@ func (s *Service) Handler() http.HandlerFunc { return s.handleWebFinger } -func (s *Service) HandleFunc(w http.ResponseWriter, r *http.Request) *ihttp.Error { +func (s *Service) HandleFunc(w http.ResponseWriter, r *http.Request) ihttp.Error { resource := r.URL.Query().Get("resource") if resource == "" { return ihttp.BadRequest("Missing resource parameter", nil)@@ -78,7 +78,7 @@ } func (s *Service) handleWebFinger(w http.ResponseWriter, r *http.Request) { if err := s.HandleFunc(w, r); err != nil { - status := err.Code + status := err.StatusCode() if status == 0 { status = http.StatusInternalServerError }
M domain/web/mux.go → domain/web/mux.go
@@ -15,19 +15,19 @@ "github.com/kevinpollet/nego" ) -func (website *Website) ErrorHandler(err *ihttp.Error, w http.ResponseWriter, r *http.Request) { +func (website *Website) ErrorHandler(err ihttp.Error, w http.ResponseWriter, r *http.Request) { if strings.Contains(r.Header.Get("Accept"), "text/html") { - w.WriteHeader(err.Code) + w.WriteHeader(err.StatusCode()) err := templates.Error(*website.siteSettings, err).Render(w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { - http.Error(w, err.Message, err.Code) + err.WriteHTTP(w) } } -func (website *Website) ServeHTTP(w http.ResponseWriter, r *http.Request) *ihttp.Error { +func (website *Website) ServeHTTP(w http.ResponseWriter, r *http.Request) ihttp.Error { urlPath := r.URL.Path if r.URL.Query().Has("go-get") && r.URL.Query().Get("go-get") == "1" { urlPath = "/go" + r.URL.Path@@ -75,7 +75,7 @@ return nil } -func (website *Website) Calendar(w http.ResponseWriter, r *http.Request) *ihttp.Error { +func (website *Website) Calendar(w http.ResponseWriter, r *http.Request) ihttp.Error { website.counter.Count(r, "Calendar") err := calendar.CalendarPage(*website.siteSettings, templates.PageSettings{ Title: "Calendar",@@ -83,11 +83,7 @@ TitleAttrs: templates.Attrs{}, BodyAttrs: templates.Attrs{}, }, *website.calendar).Render(w) if err != nil { - return &ihttp.Error{ - Code: http.StatusInternalServerError, - Message: "", - Cause: err, - } + return ihttp.InternalServerError("could not render calendar page", err) } return nil@@ -101,7 +97,7 @@ re := regexp.MustCompile( "^(.*)\\." + strings.ReplaceAll(website.config.WildcardDomain, ".", `\.`) + "$", ) replace := "${1}." + website.config.Domains[0] - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) *ihttp.Error { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) ihttp.Error { switch { case r.URL.Query().Has("go-get") && r.URL.Query().Get("go-get") == "1": return website.ServeHTTP(w, r)
M domain/web/templates/error.go → domain/web/templates/error.go
@@ -5,14 +5,14 @@ "strconv" g "alin.ovh/gomponents" . "alin.ovh/gomponents/html" - http "alin.ovh/homestead/shared/http" + "alin.ovh/homestead/shared/http" ) -func Error(site SiteSettings, err *http.Error) g.Node { +func Error(site SiteSettings, err http.Error) g.Node { return Layout(site, PageSettings{ Title: "Error", }, Div( - H1(g.Text(strconv.Itoa(err.Code)+" "+err.Message)), + H1(g.Text(strconv.Itoa(err.StatusCode())+" "+err.Message())), H2(g.Text("ʕノ•ᴥ•ʔノ ︵ ┻━┻")), )) }
M domain/web/website.go → domain/web/website.go
@@ -208,7 +208,7 @@ mux.HandleFunc("/calendar", website.Calendar) website.identity.RegisterHandlers(mux) if opts.Development { - staticHandler := func(w http.ResponseWriter, r *http.Request) *ihttp.Error { + staticHandler := func(w http.ResponseWriter, r *http.Request) ihttp.Error { http.ServeFileFS(w, r, templates.Files, r.URL.Path) return nil