fix: cast week_start to text in SELECT and add error logging for silent 500
pgx v5 cannot scan a PostgreSQL DATE column directly into a Go string. The WHERE clause already used week_start::text but the SELECT did not, causing GetByWeek to return a scan error that the GenerateMenu handler swallowed without logging (just returned 500). - repository.go: SELECT mp.week_start::text instead of mp.week_start - handler.go: add slog.Error before the silent 500 branch Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -221,6 +221,7 @@ func (h *Handler) GenerateMenu(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Return the freshly saved plan.
|
// Return the freshly saved plan.
|
||||||
plan, err := h.repo.GetByWeek(r.Context(), userID, weekStart)
|
plan, err := h.repo.GetByWeek(r.Context(), userID, weekStart)
|
||||||
if err != nil || plan == nil {
|
if err != nil || plan == nil {
|
||||||
|
slog.Error("load generated menu", "err", err, "plan_nil", plan == nil)
|
||||||
writeError(w, http.StatusInternalServerError, "failed to load generated menu")
|
writeError(w, http.StatusInternalServerError, "failed to load generated menu")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
|||||||
// Returns nil, nil when no plan exists for that week.
|
// Returns nil, nil when no plan exists for that week.
|
||||||
func (r *Repository) GetByWeek(ctx context.Context, userID, weekStart string) (*MenuPlan, error) {
|
func (r *Repository) GetByWeek(ctx context.Context, userID, weekStart string) (*MenuPlan, error) {
|
||||||
const q = `
|
const q = `
|
||||||
SELECT mp.id, mp.week_start,
|
SELECT mp.id, mp.week_start::text,
|
||||||
mi.id, mi.day_of_week, mi.meal_type,
|
mi.id, mi.day_of_week, mi.meal_type,
|
||||||
sr.id, sr.title, COALESCE(sr.image_url, ''), sr.nutrition
|
sr.id, sr.title, COALESCE(sr.image_url, ''), sr.nutrition
|
||||||
FROM menu_plans mp
|
FROM menu_plans mp
|
||||||
|
|||||||
Reference in New Issue
Block a user