1
0

Added log for a fail of retrieving entries. Added error handling and log for entries JSON encoding.

This commit is contained in:
Alexander Andreev 2022-10-21 03:22:57 +04:00
parent a000d1edad
commit d5b850d36b
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -2,6 +2,7 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"justguestbook/internal/guestbook" "justguestbook/internal/guestbook"
"justguestbook/pkg/justcaptcha" "justguestbook/pkg/justcaptcha"
"justguestbook/pkg/server" "justguestbook/pkg/server"
@ -52,6 +53,8 @@ func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
entries, err := h.db.Entries(page_num, page_size) entries, err := h.db.Entries(page_num, page_size)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("failed to retrieve entries:", err)
return
} }
guestbookEntries := struct { guestbookEntries := struct {
@ -62,7 +65,10 @@ func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
Entries: entries} Entries: entries}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(&guestbookEntries) if err := json.NewEncoder(w).Encode(&guestbookEntries); err != nil {
log.Println("failed to encode entries:", err)
http.Error(w, fmt.Sprintln("failed to encode entries:", err.Error()), http.StatusInternalServerError)
}
} }
func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) { func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {