From 6f7237f656dacdb2f5d3e040fc46c96576adc842 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sun, 5 Sep 2021 19:00:25 -0500 Subject: [PATCH] Log errors if ErrorResponse panics We have to do this because otherwise the errors never actually end up in the ResponseData. --- resetdb.sh | 26 -------------------------- src/config/types.go | 6 ++++++ src/logging/logging.go | 2 +- src/website/requesthandling.go | 7 +++++++ src/website/routes.go | 14 +++++++++----- src/website/routes_test.go | 2 +- 6 files changed, 24 insertions(+), 33 deletions(-) delete mode 100755 resetdb.sh diff --git a/resetdb.sh b/resetdb.sh deleted file mode 100755 index a79775a..0000000 --- a/resetdb.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -set -eou pipefail - -# This script is for use in local development only. It wipes the existing db, -# creates a new empty one, runs the initial migration to create the schema, -# and then imports actual db content on top of that. - -# TODO(opensource): We should adapt Asaf's seedfile command and then delete this. - -THIS_PATH=$(pwd) -BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' -# BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta' - -cd $BETA_PATH -docker-compose down -v -docker-compose up -d postgres -sleep 3 -./scripts/db_import -d -n hmn_two -c - -cd $THIS_PATH -go run src/main.go migrate 2021-03-10T05:16:21Z - -cd $BETA_PATH -./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-26 -# ./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-25 diff --git a/src/config/types.go b/src/config/types.go index bd25fa6..397c65a 100644 --- a/src/config/types.go +++ b/src/config/types.go @@ -82,6 +82,12 @@ type EpisodeGuide struct { Projects map[string]string // NOTE(asaf): Maps from slugs to default episode guide topic } +func init() { + if Config.EpisodeGuide.Projects == nil { + Config.EpisodeGuide.Projects = make(map[string]string) + } +} + func (info PostgresConfig) DSN() string { return fmt.Sprintf("user=%s password=%s host=%s port=%d dbname=%s", info.User, info.Password, info.Hostname, info.Port, info.DbName) } diff --git a/src/logging/logging.go b/src/logging/logging.go index 10c0759..74a51db 100644 --- a/src/logging/logging.go +++ b/src/logging/logging.go @@ -194,7 +194,7 @@ func LogPanicValue(logger *zerolog.Logger, val interface{}, msg string) { } if err, ok := val.(error); ok { - logger.Error().Stack().Err(err).Msg(msg) + logger.Error().Err(err).Msg(msg) } else { logger.Error(). Interface("recovered", val). diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index ec71dff..208efd3 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -276,6 +276,13 @@ func (c *RequestContext) Redirect(dest string, code int) ResponseData { } func (c *RequestContext) ErrorResponse(status int, errs ...error) ResponseData { + defer func() { + if r := recover(); r != nil { + LogContextErrors(c, errs...) + panic(r) + } + }() + res := ResponseData{ StatusCode: status, Errors: errs, diff --git a/src/website/routes.go b/src/website/routes.go index eaed141..ee74dbb 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -37,7 +37,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, &res) + defer LogContextErrorsFromResponse(c, &res) defer MiddlewarePanicCatcher(c, &res) return h(c) @@ -53,7 +53,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, &res) + defer LogContextErrorsFromResponse(c, &res) defer MiddlewarePanicCatcher(c, &res) defer storeNoticesInCookie(c, &res) @@ -75,7 +75,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, &res) + defer LogContextErrorsFromResponse(c, &res) defer MiddlewarePanicCatcher(c, &res) defer storeNoticesInCookie(c, &res) @@ -558,12 +558,16 @@ func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (aft } } -func LogContextErrors(c *RequestContext, res *ResponseData) { - for _, err := range res.Errors { +func LogContextErrors(c *RequestContext, errs ...error) { + for _, err := range errs { c.Logger.Error().Timestamp().Stack().Str("Requested", c.FullUrl()).Err(err).Msg("error occurred during request") } } +func LogContextErrorsFromResponse(c *RequestContext, res *ResponseData) { + LogContextErrors(c, res.Errors...) +} + func MiddlewarePanicCatcher(c *RequestContext, res *ResponseData) { if recovered := recover(); recovered != nil { maybeError, ok := recovered.(*error) diff --git a/src/website/routes_test.go b/src/website/routes_test.go index bbd822a..a706c5e 100644 --- a/src/website/routes_test.go +++ b/src/website/routes_test.go @@ -28,7 +28,7 @@ func TestLogContextErrors(t *testing.T) { Middleware: func(h Handler) Handler { return func(c *RequestContext) (res ResponseData) { c.Logger = &logger - defer LogContextErrors(c, &res) + defer LogContextErrorsFromResponse(c, &res) return h(c) } },