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"
)
var ErrNotFound = errors.New("not found")
type GenString func(length int) (string, error)
type Store struct {
@ -211,8 +213,7 @@ func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuer
return GetEventMetadataResult{}, err
}
if !found {
// TODO return a constant or a specific error type for Not Found
return GetEventMetadataResult{}, errors.New("not found")
return GetEventMetadataResult{}, ErrNotFound
}
return result, nil
}
@ -246,7 +247,7 @@ func (s *Store) GetEventResponseSummary(ctx context.Context, query GetEventRespo
return GetEventResponseSummaryResult{}, err
}
if !found {
return GetEventResponseSummaryResult{}, errors.New("event not found")
return GetEventResponseSummaryResult{}, ErrNotFound
}
var result GetEventResponseSummaryResult
@ -351,8 +352,7 @@ func (s *Store) GetEventResponse(ctx context.Context, query GetEventResponseQuer
return GetEventResponseResult{}, err
}
if !found {
// TODO return a constant or typed error
return GetEventResponseResult{}, errors.New("not found")
return GetEventResponseResult{}, ErrNotFound
}
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) {
eventAlphaID := r.URL.Query().Get(fieldNameEventID)
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventAlphaID,
})
// TODO return 404 if event not found
if err != nil {
fmt.Fprint(w, err)
if notFound(w, err, "Event not found") || internalServerError(w, err) {
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) {
err := r.ParseForm()
if err != nil {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
if invalidForm(w, err) {
return
}
@ -161,28 +189,20 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
var dateString string
var hour int
_, err := fmt.Sscanf(dateHourString, "%8s-%02d", &dateString, &hour)
if err != nil {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
if invalidForm(w, err) {
return
}
d, err := date.Parse("20060102", dateString)
if err != nil {
log.Println(err)
http.Error(w, "invalid form values", http.StatusBadRequest)
if invalidForm(w, err) {
return
}
fmt.Println("parsed date/hour:", d, hour)
if cmd.DateHours[d] == nil {
cmd.DateHours[d] = make(map[int]struct{})
}
cmd.DateHours[d][hour] = struct{}{}
}
eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd)
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
if notFound(w, err, "Event not found") || internalServerError(w, err) {
return
}
@ -197,20 +217,14 @@ func (h *handler) handleVoteSuccess(w http.ResponseWriter, r *http.Request) {
EventAlphaID: r.URL.Query().Get(fieldNameEventID),
ResponseAlphaID: r.URL.Query().Get(fieldNameResponseID),
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
if notFound(w, err, "Event response not found.") || internalServerError(w, err) {
return
}
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: r.URL.Query().Get(fieldNameEventID),
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
if notFound(w, err, "Event not found") || internalServerError(w, err) {
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) {
// TODO consider redirecting to admin
earliest, err := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
if err != nil {
fmt.Fprint(w, "bad earliest date")
if invalidForm(w, err) {
return
}
latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest))
if err != nil {
fmt.Fprint(w, "bad latest date")
if invalidForm(w, err) {
return
}
eventName := r.FormValue(fieldNameEventName)
if eventName == "" {
fmt.Fprint(w, "event name is required")
fmt.Fprint(w, "Event name is required")
return
}
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{
AlphaID: eventID,
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
if notFound(w, err, "Event not found") || internalServerError(w, err) {
return
}
@ -346,9 +355,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventID,
})
// TODO return 404 if event not found
if err != nil {
fmt.Fprint(w, err)
if notFound(w, err, "Event not found") || internalServerError(w, err) {
return
}