all repos — homestead @ c7165476f375b097761e729ac14f9b8a68af98c6

Code for my website

simplify HTTP error module

Alan Pearce
commit

c7165476f375b097761e729ac14f9b8a68af98c6

parent

8267148e988f22647aa735652dafd06c692b047f

M domain/content/publisher/mux.godomain/content/publisher/mux.go
@@ -12,12 +12,17 @@ )
type user struct{} +var ( + ErrCannotDetermineUser = ihttp.NewError("cannot determine user", http.StatusInternalServerError) + ErrRenderFailure = ihttp.NewError("failed to render page", http.StatusInternalServerError) +) + func (app *App) WithUserContext(fn ihttp.HandleFunc) ihttp.HandleFunc { return func(w http.ResponseWriter, r *http.Request) ihttp.Error { ctx := r.Context() who, err := app.localClient.WhoIs(ctx, r.RemoteAddr) if err != nil { - return ihttp.InternalServerError("cannot determine user", err) + return ErrCannotDetermineUser } return fn(w, r.WithContext(
@@ -35,7 +40,7 @@ },
User: user.LoginName, }).Render(w) if err != nil { - return ihttp.InternalServerError("Failed to render index page", err) + return ErrRenderFailure } return nil
M domain/identity/webfinger/service.godomain/identity/webfinger/service.go
@@ -21,6 +21,18 @@ providers []ResourceProvider
corsOrigin string } +var ( + ErrMissingResourceParameter = ihttp.NewError( + "Missing resource parameter", + http.StatusBadRequest, + ) + ErrFailedToEncodeResponse = ihttp.NewError( + "Failed to encode webfinger response", + http.StatusInternalServerError, + ) + ErrNotFound = ihttp.NewError("Resource not found", http.StatusNotFound) +) + type Option func(*Service) func WithCORSOrigin(origin string) Option {
@@ -50,7 +62,7 @@
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) + return ErrMissingResourceParameter } for _, provider := range s.providers {
@@ -62,12 +74,12 @@ w.Header().Add("Access-Control-Allow-Origin", s.corsOrigin)
} if err := json.NewEncoder(w).Encode(provider.GetIdentityResource()); err != nil { - return ihttp.InternalServerError("Failed to encode webfinger response", err) + return ErrFailedToEncodeResponse.WithCause(err) } return nil } } - return ihttp.NotFound("Resource not found") + return ErrNotFound }
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
M shared/http/error.goshared/http/error.go
@@ -18,7 +18,7 @@ message string
cause error } -func (e *httpError) Error() string { +func (e httpError) Error() string { if e.message == "" { e.message = http.StatusText(e.code) }
@@ -26,11 +26,11 @@
return fmt.Sprintf("%d %s", e.code, e.message) } -func (e *httpError) StatusCode() int { +func (e httpError) StatusCode() int { return e.code } -func (e *httpError) Message() string { +func (e httpError) Message() string { if e.message == "" { e.message = http.StatusText(e.code) }
@@ -38,53 +38,21 @@
return e.message } -func (e *httpError) Unwrap() error { +func (e httpError) Unwrap() error { return e.cause } -func NewError(code int, message string, cause error) Error { - return &httpError{ +func NewError(message string, code int) httpError { + return httpError{ code: code, message: message, - cause: cause, } } -func NotFound(message string) Error { - return &httpError{ - code: http.StatusNotFound, - message: message, - } -} - -func BadRequest(message string, cause error) Error { - return &httpError{ - code: http.StatusBadRequest, - message: message, - cause: cause, - } -} - -func InternalServerError(message string, cause error) Error { - return &httpError{ - code: http.StatusInternalServerError, - message: message, - cause: cause, - } -} - -func Unauthorized(message string, cause error) Error { - return &httpError{ - code: http.StatusUnauthorized, - message: message, - cause: cause, - } -} - -func Forbidden(message string, cause error) Error { - return &httpError{ - code: http.StatusForbidden, - message: message, +func (e httpError) WithCause(cause error) Error { + return httpError{ + code: e.code, + message: e.message, cause: cause, } }