Add some 404s

This commit is contained in:
Brandon Dyck 2020-09-27 21:40:55 -06:00
parent fa330e4397
commit 15a3d293e6
2 changed files with 49 additions and 42 deletions

View File

@ -12,6 +12,8 @@ import (
"github.com/rickb777/date" "github.com/rickb777/date"
) )
var ErrNotFound = errors.New("not found")
type GenString func(length int) (string, error) type GenString func(length int) (string, error)
type Store struct { type Store struct {
@ -211,8 +213,7 @@ func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuer
return GetEventMetadataResult{}, err return GetEventMetadataResult{}, err
} }
if !found { if !found {
// TODO return a constant or a specific error type for Not Found return GetEventMetadataResult{}, ErrNotFound
return GetEventMetadataResult{}, errors.New("not found")
} }
return result, nil return result, nil
} }
@ -246,7 +247,7 @@ func (s *Store) GetEventResponseSummary(ctx context.Context, query GetEventRespo
return GetEventResponseSummaryResult{}, err return GetEventResponseSummaryResult{}, err
} }
if !found { if !found {
return GetEventResponseSummaryResult{}, errors.New("event not found") return GetEventResponseSummaryResult{}, ErrNotFound
} }
var result GetEventResponseSummaryResult var result GetEventResponseSummaryResult
@ -351,8 +352,7 @@ func (s *Store) GetEventResponse(ctx context.Context, query GetEventResponseQuer
return GetEventResponseResult{}, err return GetEventResponseResult{}, err
} }
if !found { if !found {
// TODO return a constant or typed error return GetEventResponseResult{}, ErrNotFound
return GetEventResponseResult{}, errors.New("not found")
} }
result.DateHours = make(map[date.Date]map[int]struct{}) result.DateHours = make(map[date.Date]map[int]struct{})

View File

@ -119,14 +119,44 @@ func voteForm(disabled bool, st voteState) hm.Term {
) )
} }
func notFound(w http.ResponseWriter, err error, msg string) bool {
if err == back.ErrNotFound {
http.Error(w, msg, http.StatusNotFound)
return true
}
return false
}
func invalidForm(w http.ResponseWriter, err error) bool {
if err != nil {
http.Error(w,
"Invalid form values. That shouldn't have been possible. Please try again later.",
http.StatusBadRequest)
logError(err)
return true
}
return false
}
func logError(err error) {
log.Println("ERROR:", err.Error())
}
func internalServerError(w http.ResponseWriter, err error) bool {
if err != nil {
http.Error(w, "Something went wrong. Please try again later.", http.StatusInternalServerError)
logError(err)
return true
}
return false
}
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) { func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
eventAlphaID := r.URL.Query().Get(fieldNameEventID) eventAlphaID := r.URL.Query().Get(fieldNameEventID)
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{ event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventAlphaID, AlphaID: eventAlphaID,
}) })
// TODO return 404 if event not found if notFound(w, err, "Event not found") || internalServerError(w, err) {
if err != nil {
fmt.Fprint(w, err)
return return
} }
@ -144,9 +174,7 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if invalidForm(w, err) {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
return return
} }
@ -161,28 +189,20 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
var dateString string var dateString string
var hour int var hour int
_, err := fmt.Sscanf(dateHourString, "%8s-%02d", &dateString, &hour) _, err := fmt.Sscanf(dateHourString, "%8s-%02d", &dateString, &hour)
if err != nil { if invalidForm(w, err) {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
return return
} }
d, err := date.Parse("20060102", dateString) d, err := date.Parse("20060102", dateString)
if err != nil { if invalidForm(w, err) {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
return return
} }
fmt.Println("parsed date/hour:", d, hour)
if cmd.DateHours[d] == nil { if cmd.DateHours[d] == nil {
cmd.DateHours[d] = make(map[int]struct{}) cmd.DateHours[d] = make(map[int]struct{})
} }
cmd.DateHours[d][hour] = struct{}{} cmd.DateHours[d][hour] = struct{}{}
} }
eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd) eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd)
if err != nil { if notFound(w, err, "Event not found") || internalServerError(w, err) {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return return
} }
@ -197,20 +217,14 @@ func (h *handler) handleVoteSuccess(w http.ResponseWriter, r *http.Request) {
EventAlphaID: r.URL.Query().Get(fieldNameEventID), EventAlphaID: r.URL.Query().Get(fieldNameEventID),
ResponseAlphaID: r.URL.Query().Get(fieldNameResponseID), ResponseAlphaID: r.URL.Query().Get(fieldNameResponseID),
}) })
if err != nil { if notFound(w, err, "Event response not found.") || internalServerError(w, err) {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return return
} }
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{ event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: r.URL.Query().Get(fieldNameEventID), AlphaID: r.URL.Query().Get(fieldNameEventID),
}) })
if err != nil { if notFound(w, err, "Event not found") || internalServerError(w, err) {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return return
} }
@ -256,18 +270,16 @@ func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
// TODO consider redirecting to admin // TODO consider redirecting to admin
earliest, err := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest)) earliest, err := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
if err != nil { if invalidForm(w, err) {
fmt.Fprint(w, "bad earliest date")
return return
} }
latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest)) latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest))
if err != nil { if invalidForm(w, err) {
fmt.Fprint(w, "bad latest date")
return return
} }
eventName := r.FormValue(fieldNameEventName) eventName := r.FormValue(fieldNameEventName)
if eventName == "" { if eventName == "" {
fmt.Fprint(w, "event name is required") fmt.Fprint(w, "Event name is required")
return return
} }
description := r.FormValue(fieldNameDescription) description := r.FormValue(fieldNameDescription)
@ -294,10 +306,7 @@ func (h *handler) handleCreateSuccess(w http.ResponseWriter, r *http.Request) {
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{ event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: eventID, AlphaID: eventID,
}) })
if err != nil { if notFound(w, err, "Event not found") || internalServerError(w, err) {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return return
} }
@ -346,9 +355,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{ metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventID, AlphaID: eventID,
}) })
// TODO return 404 if event not found if notFound(w, err, "Event not found") || internalServerError(w, err) {
if err != nil {
fmt.Fprint(w, err)
return return
} }