Added log for a fail of retrieving entries. Added error handling and log for entries JSON encoding.
This commit is contained in:
parent
a000d1edad
commit
d5b850d36b
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user