all repos — homestead @ c7165476f375b097761e729ac14f9b8a68af98c6

Code for my website

simplify HTTP error module

Alan Pearce
commit

c7165476f375b097761e729ac14f9b8a68af98c6

parent

8267148e988f22647aa735652dafd06c692b047f

1 file changed, 9 insertions(+), 3 deletions(-)

changed files
M domain/web/mux.godomain/web/mux.go
@@ -15,6 +15,12 @@
"github.com/kevinpollet/nego" ) +var ( + ErrReadingFile = ihttp.NewError("Error reading file", http.StatusInternalServerError) + ErrNotFound = ihttp.NewError("File not found", http.StatusNotFound) + ErrRenderFailure = ihttp.NewError("Error rendering template", http.StatusInternalServerError) +) + 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.StatusCode())
@@ -42,10 +48,10 @@ file, err := website.reader.GetFile(urlPath)
if err != nil { website.log.Warn("Error reading file", "error", err) - return ihttp.InternalServerError("Error reading file", err) + return ErrReadingFile.WithCause(err) } if file == nil { - return ihttp.NotFound("File not found") + return ErrNotFound } analytics.WithTitle(r, file.Title) w.Header().Add("ETag", file.Etag)
@@ -72,7 +78,7 @@ func (website *Website) Calendar(w http.ResponseWriter, r *http.Request) ihttp.Error {
analytics.WithTitle(r, "Calendar") err := calendar.CalendarPage(*website.siteSettings, website.calendar).Render(w) if err != nil { - return ihttp.InternalServerError("could not render calendar page", err) + return ErrRenderFailure.WithCause(err) } return nil