1
0
Fork 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
1 changed files with 7 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package handlers
import (
"encoding/json"
"fmt"
"justguestbook/internal/guestbook"
"justguestbook/pkg/justcaptcha"
"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)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("failed to retrieve entries:", err)
return
}
guestbookEntries := struct {
@ -62,7 +65,10 @@ func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
Entries: entries}
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) {